Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Roblox Script to create combat system
- -- Client Side
- local Players = game:GetService("Players")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local UIS = game:GetService("UserInputService")
- -- Getting Player, Character and his Mouse
- local player = Players.LocalPlayer
- local mouse = player:GetMouse()
- local character = player.Character
- -- waiting for the character to load
- if not character or not character.Parent then
- character = player.CharacterAdded:Wait()
- end
- local humanoid = character:WaitForChild("Humanoid")
- local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
- local animator = humanoid:WaitForChild("Animator")
- local punchEvent = ReplicatedStorage.punchEvent
- local PunchTool = game.StarterPack.Punch
- -- Getting animations for combat system
- local BlockAnimation = ReplicatedStorage.Animations.R15Block
- local LeftPunchAnimation = ReplicatedStorage.Animations.R15LeftPunch
- local RightPunchAnimation = ReplicatedStorage.Animations.R15RightPunch
- -- we need to load animation before we use them
- local BlockAnimationTrack = animator:LoadAnimation(BlockAnimation)
- local LeftAnimationTrack = animator:LoadAnimation(LeftPunchAnimation)
- local RightAnimationTrack = animator:LoadAnimation(RightPunchAnimation)
- local combo = 1
- local block = false
- local punching = false
- local function LeftPunch()
- --[[ 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.
- ]]--
- if not block and not punching and combo == 1 and mouse.Target.Name ~= "Button" and mouse.Target.Name ~= "ClickPartForPathService" then
- punching = true -- set punching to true
- combo = 2
- LeftAnimationTrack:Play() -- play left hand punch animation
- punchEvent:FireServer() -- Trigger the server to apply damage to another player when the player punches.
- wait(0.5) -- wait for 0.5sec before stopping animation
- LeftAnimationTrack:Stop() -- stop left hand punch animation
- punching = false -- set punching to false
- end
- end
- -- this is the same as the upper one but it runs when combo == 2
- local function RightPunch()
- if not block and not punching and combo == 2 and mouse.Target.Name ~= "Button" and mouse.Target.Name ~= "ClickPartForPathService" then
- punching = true -- set punching to true
- combo = 1
- RightAnimationTrack:Play() -- play right hand punch animation
- punchEvent:FireServer() -- Trigger the server to apply damage to another player when the player punches.
- wait(0.5) -- wait for 0.5sec before stopping animation
- RightAnimationTrack:Stop() -- stop right hand punch animation
- punching = false -- set punching to false
- end
- end
- -- this function play block animation whenever function called
- local function Block()
- if not block and not punching then
- block = true
- BlockAnimationTrack:Play()
- end
- end
- -- this function stop block animation whenever function called
- local function UnBlock()
- if block and not punching then
- block = false
- BlockAnimationTrack:Stop()
- end
- end
- -- this is calling the RightPunch function when player click right mouse buttton
- UIS.InputBegan:Connect(function(input, gameProcessedEvent)
- if not gameProcessedEvent and input.UserInputType == Enum.UserInputType.MouseButton1 then
- RightPunch()
- end
- end)
- -- this is calling the leftPunch function when player click right mouse buttton
- UIS.InputBegan:Connect(function(input, gameProcessedEvent)
- if not gameProcessedEvent and input.UserInputType == Enum.UserInputType.MouseButton1 then
- LeftPunch()
- end
- end)
- -- this is calling the block function when player click left mouse buttton
- UIS.InputBegan:Connect(function(input, gameProcessedEvent)
- if not gameProcessedEvent and input.UserInputType == Enum.UserInputType.MouseButton2 then
- Block()
- end
- end)
- -- this is calling the UnBlock function when player click left mouse buttton
- UIS.InputEnded:Connect(function(input, gameProcessedEvent)
- if not gameProcessedEvent and input.UserInputType == Enum.UserInputType.MouseButton2 then
- UnBlock()
- end
- end)
- -- Server Script
- -- This function runs whenever a player joins the game, and its "player" parameter corresponds to that specific player.
- game.Players.PlayerAdded:Connect(function(player)
- -- This function executes when a player character is added to the game, and its first parameter is the specific player character being referred to.
- player.CharacterAdded:Connect(function(char)
- local humanoid = char:WaitForChild("Humanoid")
- local humanoidRootPart = char:WaitForChild("HumanoidRootPart")
- local hitChars = {}
- local Swinging = false
- local VisualEffects = true
- local SoundEnabled = true
- local Damage = 10
- game:GetService("RunService").Heartbeat:Connect(function() -- this is the heartbeat event of run service which run every frame
- local animations = humanoid:GetPlayingAnimationTracks() -- it will get the animation tracks of humanoid
- local Attacking = false
- local AttackAnim = 1
- for i, animation in pairs(animations) do -- it will get all the animations from animation track of humanoid
- if animation.Animation.AnimationId == game.ReplicatedStorage.Animations.R15Block.AnimationId then
- Attacking = false
- end
- if animation.Animation.AnimationId == game.ReplicatedStorage.Animations.R15LeftPunch.AnimationId then
- AttackAnim = 1
- Attacking = true
- end
- if animation.Animation.AnimationId == game.ReplicatedStorage.Animations.R15RightPunch.AnimationId then
- AttackAnim = 2
- Attacking = true
- end
- end
- if Attacking == true then
- --[[ It will shoot out a beam starting from the position of the humanoid's root part, going in a particular direction,
- and extending a distance of 3 studs.
- ]]--
- local ray = Ray.new(humanoidRootPart.Position, humanoidRootPart.CFrame.LookVector * 3)
- local part, position = game.Workspace:FindPartOnRay(ray, char) -- it finds the part on ray
- if part and part.Parent:FindFirstChild("Humanoid") and not hitChars[part.Parent] then
- local Torso
- if part.Parent:FindFirstChild("Torso") then
- Torso = part.Parent:FindFirstChild("Torso")
- end
- if part.Parent:FindFirstChild("UpperTorso") then
- Torso = part.Parent:FindFirstChild("UpperTorso")
- end
- local EnemyAnimations = part.Parent.Humanoid:GetPlayingAnimationTracks() --[[ It will find the animation being used
- by the enemy's character, the one who got punched by the player. ]]--
- local Blocking = false
- -- it will get animation id of enemy and check if it's match with the block animation which is in ReplicatedStorage
- for i, animation in pairs(EnemyAnimations) do
- if animation.Animation.AnimationId == game.ReplicatedStorage.Animations.R15Block.AnimationId then
- Blocking = true
- end
- end
- if Blocking == false then
- hitChars[part.Parent] = true
- -- It applies a force to the enemy character's central body part (HumanoidRootPart) to push them away.
- local bv = Instance.new("BodyVelocity")
- bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
- bv.Velocity = humanoidRootPart.CFrame.LookVector * 50 + Vector3.new(0, 10, 0)
- bv.Parent = part.Parent.HumanoidRootPart
- game:GetService("Debris"):AddItem(bv, 0.1)
- if VisualEffects == true then
- --[[ It will check whether the torso is already have a "Hit Effect." If it is absent, the code will duplicate
- the "hit effect" from the storage where replicated items are kept (ReplicatedStorage) and attach it to the
- torso for a brief period of 2 seconds. ]]--
- if not Torso:FindFirstChild("HitEffect") then
- local HitEffect = game.ReplicatedStorage.Effects.HitEffect:Clone()
- HitEffect.Parent = Torso
- game:GetService("Debris"):AddItem(HitEffect, 2)
- end
- end
- if SoundEnabled then
- if AttackAnim == 1 then
- -- it will clone left hand Punch sound from the ReplicatedStorage and add it to the HumanoidRootPart for 1 sec
- local PunchSound = game.ReplicatedStorage.Sounds.Punched1:Clone()
- PunchSound.Playing = true
- PunchSound.Parent = char.HumanoidRootPart
- game:GetService("Debris"):AddItem(PunchSound, 1)
- else
- -- it will clone Right hand Punch sound from the ReplicatedStorage and add it to the HumanoidRootPart for 1 sec
- local RightPunch = game.ReplicatedStorage.Sounds.Punched2:Clone()
- RightPunch.Playing = true
- RightPunch.Parent = char.HumanoidRootPart
- game:GetService("Debris"):AddItem(RightPunch, 1)
- end
- end
- part.Parent.Humanoid:TakeDamage(Damage) -- it will give damage to the enemy humanoid
- wait(0.6)
- hitChars[part.Parent] = nil
- else
- -- this will clone the Block Effects from ReplicatedStorage and add it into enemy torso for 2 sec
- local BlockEffect = game.ReplicatedStorage.Effects.BlockEffect:Clone()
- BlockEffect.Parent = Torso
- game:GetService("Debris"):AddItem(BlockEffect, 2)
- end
- end
- end
- end)
- end)
- end)
- -- It will trigger a sound named "swing" to play each time the player delivers a punch.
- game.ReplicatedStorage.punchEvent.OnServerEvent:Connect(function(plr)
- if game.Workspace[plr.Name].HumanoidRootPart:FindFirstChild("Swing") then
- game.Workspace[plr.Name].HumanoidRootPart.Swing:Play()
- else
- game.ReplicatedStorage.Sounds.Swing:Clone().Parent = game.Workspace[plr.Name].HumanoidRootPart
- game.Workspace[plr.Name].HumanoidRootPart.Swing:Play()
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement