Advertisement
Scripting_King

Untitled

Aug 10th, 2023
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.75 KB | Source Code | 0 0
  1. -- Roblox Script to create combat system
  2.  
  3. -- Client Side
  4. local Players = game:GetService("Players")
  5. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  6. local UIS = game:GetService("UserInputService")
  7.  
  8. -- Getting Player, Character and his Mouse
  9. local player = Players.LocalPlayer
  10. local mouse = player:GetMouse()
  11. local character = player.Character
  12. -- waiting for the character to load
  13. if not character or not character.Parent then
  14.     character = player.CharacterAdded:Wait()
  15. end
  16.  
  17.  
  18. local humanoid = character:WaitForChild("Humanoid")
  19. local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
  20. local animator = humanoid:WaitForChild("Animator")
  21. local punchEvent = ReplicatedStorage.punchEvent
  22. local PunchTool = game.StarterPack.Punch
  23.  
  24.  
  25. -- Getting animations for combat system
  26. local BlockAnimation = ReplicatedStorage.Animations.R15Block
  27. local LeftPunchAnimation = ReplicatedStorage.Animations.R15LeftPunch
  28. local RightPunchAnimation = ReplicatedStorage.Animations.R15RightPunch
  29.  
  30. -- we need to load animation before we use them
  31. local BlockAnimationTrack = animator:LoadAnimation(BlockAnimation)
  32. local LeftAnimationTrack = animator:LoadAnimation(LeftPunchAnimation)
  33. local RightAnimationTrack = animator:LoadAnimation(RightPunchAnimation)
  34.  
  35.  
  36. local combo = 1
  37. local block = false
  38. local punching = false
  39.  
  40.  
  41. local function LeftPunch()
  42. --[[ it is conditional statement to Make sure it's working right by checking if the player isn't blocking or punching, if the combo is equal to 1, and if the mouse isn't hitting the button or the "ClickPartForPathService" part.
  43. ]]--
  44.     if not block and not punching and combo == 1 and mouse.Target.Name ~= "Button" and mouse.Target.Name ~= "ClickPartForPathService" then
  45.         punching = true -- set punching to true
  46.         combo = 2
  47.         LeftAnimationTrack:Play() -- play left hand punch animation
  48.         punchEvent:FireServer() -- Trigger the server to apply damage to another player when the player punches.
  49.         wait(0.5)  -- wait for 0.5sec before stopping animation
  50.         LeftAnimationTrack:Stop()  -- stop left hand punch animation
  51.         punching = false  -- set punching to false
  52.     end
  53.  
  54. end
  55. -- this is the same as the upper one but it runs when combo == 2
  56. local function RightPunch()
  57.    
  58.     if not block and not punching and combo == 2 and mouse.Target.Name ~= "Button" and mouse.Target.Name ~= "ClickPartForPathService" then
  59.         punching = true -- set punching to true
  60.         combo = 1
  61.         RightAnimationTrack:Play()  -- play right hand punch animation
  62.         punchEvent:FireServer()  -- Trigger the server to apply damage to another player when the player punches.
  63.         wait(0.5)  -- wait for 0.5sec before stopping animation
  64.         RightAnimationTrack:Stop()  -- stop right hand punch animation
  65.         punching = false  -- set punching to false
  66.     end
  67.  
  68. end
  69.  
  70. -- this function play block animation whenever function called
  71. local function Block()
  72.     if not block and not punching then
  73.         block = true
  74.         BlockAnimationTrack:Play()
  75.     end
  76. end
  77.  
  78. -- this function stop block animation whenever function called
  79. local function UnBlock()
  80.     if block and not punching then
  81.         block = false
  82.         BlockAnimationTrack:Stop()
  83.     end
  84. end
  85.  
  86.  
  87. -- this is calling the RightPunch function when player click right mouse buttton
  88. UIS.InputBegan:Connect(function(input, gameProcessedEvent)
  89.     if not gameProcessedEvent and input.UserInputType == Enum.UserInputType.MouseButton1 then
  90.         RightPunch()
  91.     end
  92. end)
  93.  
  94. -- this is calling the leftPunch function when player click right mouse buttton
  95. UIS.InputBegan:Connect(function(input, gameProcessedEvent)
  96.     if not gameProcessedEvent and input.UserInputType == Enum.UserInputType.MouseButton1 then
  97.         LeftPunch()
  98.     end
  99. end)
  100.  
  101.  
  102. -- this is calling the block function when player click left mouse buttton
  103. UIS.InputBegan:Connect(function(input, gameProcessedEvent)
  104.     if not gameProcessedEvent and input.UserInputType == Enum.UserInputType.MouseButton2 then
  105.         Block()
  106.     end
  107. end)
  108.  
  109. -- this is calling the UnBlock function when player click left mouse buttton
  110. UIS.InputEnded:Connect(function(input, gameProcessedEvent)
  111.     if not gameProcessedEvent and input.UserInputType == Enum.UserInputType.MouseButton2 then
  112.         UnBlock()
  113.     end
  114. end)
  115.  
  116.  
  117. -- Server Script
  118.  
  119. -- This function runs whenever a player joins the game, and its "player" parameter corresponds to that specific player.
  120. game.Players.PlayerAdded:Connect(function(player)
  121. -- This function executes when a player character is added to the game, and its first parameter is the specific player character being referred to.
  122.     player.CharacterAdded:Connect(function(char)
  123.         local humanoid = char:WaitForChild("Humanoid")
  124.         local humanoidRootPart = char:WaitForChild("HumanoidRootPart")
  125.         local hitChars = {}
  126.         local Swinging = false
  127.         local VisualEffects = true
  128.         local SoundEnabled = true
  129.         local Damage = 10
  130.         game:GetService("RunService").Heartbeat:Connect(function() -- this is the heartbeat event of run service which run every frame
  131.            
  132.             local animations = humanoid:GetPlayingAnimationTracks() -- it will get the animation tracks of humanoid
  133.             local Attacking = false
  134.             local AttackAnim = 1
  135.  
  136.             for i, animation in pairs(animations) do -- it will get all the animations from animation track of humanoid
  137.                 if animation.Animation.AnimationId == game.ReplicatedStorage.Animations.R15Block.AnimationId then
  138.                     Attacking = false
  139.                 end
  140.                 if animation.Animation.AnimationId == game.ReplicatedStorage.Animations.R15LeftPunch.AnimationId then
  141.                     AttackAnim = 1
  142.                     Attacking = true
  143.                 end
  144.                 if animation.Animation.AnimationId == game.ReplicatedStorage.Animations.R15RightPunch.AnimationId then
  145.                     AttackAnim = 2
  146.                     Attacking = true
  147.                 end
  148.             end
  149.  
  150.             if Attacking == true then
  151.                 --[[ It will shoot out a beam starting from the position of the humanoid's root part, going in a particular direction,
  152.                    and extending a distance of 3 studs.
  153.                 ]]--  
  154.                 local ray = Ray.new(humanoidRootPart.Position, humanoidRootPart.CFrame.LookVector * 3)
  155.                 local part, position = game.Workspace:FindPartOnRay(ray, char) -- it finds the part on ray
  156.                 if part and part.Parent:FindFirstChild("Humanoid") and not hitChars[part.Parent] then
  157.                     local Torso
  158.                     if part.Parent:FindFirstChild("Torso") then
  159.                         Torso = part.Parent:FindFirstChild("Torso")
  160.                     end
  161.                     if part.Parent:FindFirstChild("UpperTorso") then
  162.                         Torso = part.Parent:FindFirstChild("UpperTorso")
  163.                     end
  164.                     local EnemyAnimations = part.Parent.Humanoid:GetPlayingAnimationTracks() --[[ It will find the animation being used
  165.                     by the enemy's character, the one who got punched by the player. ]]--
  166.                     local Blocking = false
  167.                     -- it will get animation id of enemy and check if it's match with the block animation which is in ReplicatedStorage
  168.                     for i, animation in pairs(EnemyAnimations) do
  169.                         if animation.Animation.AnimationId == game.ReplicatedStorage.Animations.R15Block.AnimationId then
  170.                             Blocking = true
  171.                         end
  172.                     end
  173.                     if Blocking == false then
  174.                         hitChars[part.Parent] = true
  175.                         -- It applies a force to the enemy character's central body part (HumanoidRootPart) to push them away.
  176.                         local bv = Instance.new("BodyVelocity")
  177.                         bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
  178.                         bv.Velocity = humanoidRootPart.CFrame.LookVector * 50 + Vector3.new(0, 10, 0)
  179.                         bv.Parent = part.Parent.HumanoidRootPart
  180.                         game:GetService("Debris"):AddItem(bv, 0.1)
  181.                         if VisualEffects == true then
  182.                         --[[ It will check whether the torso is already have a "Hit Effect." If it is absent, the code will duplicate
  183.                         the "hit effect" from the storage where replicated items are kept (ReplicatedStorage) and attach it to the
  184.                         torso for a brief period of 2 seconds. ]]--
  185.                             if not Torso:FindFirstChild("HitEffect") then
  186.                                 local HitEffect = game.ReplicatedStorage.Effects.HitEffect:Clone()
  187.                                 HitEffect.Parent = Torso
  188.                                 game:GetService("Debris"):AddItem(HitEffect, 2)
  189.                             end
  190.                         end
  191.                         if SoundEnabled then
  192.                             if AttackAnim == 1 then
  193.                          -- it will clone left hand Punch sound from the ReplicatedStorage and add it to the HumanoidRootPart for 1 sec
  194.                                 local PunchSound = game.ReplicatedStorage.Sounds.Punched1:Clone()
  195.                                 PunchSound.Playing = true
  196.                                 PunchSound.Parent = char.HumanoidRootPart
  197.                                 game:GetService("Debris"):AddItem(PunchSound, 1)
  198.                             else
  199.                         -- it will clone Right hand Punch sound from the ReplicatedStorage and add it to the HumanoidRootPart for 1 sec
  200.                                 local RightPunch = game.ReplicatedStorage.Sounds.Punched2:Clone()
  201.                                 RightPunch.Playing = true
  202.                                 RightPunch.Parent = char.HumanoidRootPart
  203.                                 game:GetService("Debris"):AddItem(RightPunch, 1)
  204.                             end
  205.                         end
  206.                         part.Parent.Humanoid:TakeDamage(Damage) -- it will give damage to the enemy humanoid
  207.                         wait(0.6)
  208.                         hitChars[part.Parent] = nil
  209.                     else
  210.                       -- this will clone the  Block Effects from ReplicatedStorage and add it into enemy torso for 2 sec
  211.                         local BlockEffect = game.ReplicatedStorage.Effects.BlockEffect:Clone()
  212.                         BlockEffect.Parent = Torso
  213.                         game:GetService("Debris"):AddItem(BlockEffect, 2)
  214.                     end
  215.                 end
  216.             end
  217.         end)
  218.     end)
  219. end)
  220.  
  221. -- It will trigger a sound named "swing" to play each time the player delivers a punch.
  222. game.ReplicatedStorage.punchEvent.OnServerEvent:Connect(function(plr)
  223.     if game.Workspace[plr.Name].HumanoidRootPart:FindFirstChild("Swing") then
  224.         game.Workspace[plr.Name].HumanoidRootPart.Swing:Play()
  225.     else
  226.         game.ReplicatedStorage.Sounds.Swing:Clone().Parent = game.Workspace[plr.Name].HumanoidRootPart
  227.         game.Workspace[plr.Name].HumanoidRootPart.Swing:Play()
  228.     end
  229. end)
  230.  
  231.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement