How to make this punching script deal damage

Hello, Heres the code in question

local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Mouse = Player:GetMouse()
local PunchAnim = Humanoid:LoadAnimation(script:WaitForChild("Punch1Anim"))
local PunchAnim2 = Humanoid:LoadAnimation(script:WaitForChild("Punch2Anim"))
local KickAnim = Humanoid:LoadAnimation(script:WaitForChild("Kick1Anim"))
local RightArm = game.Players.LocalPlayer.Character["Right Arm"]
local LeftArm = game.Players.LocalPlayer.Character["Left Arm"]
local Leftleg = game.Players.LocalPlayer.Character["Left Leg"]



local NextAnim = 1 
local Cooldown = false

Mouse.Button1Down:Connect(function()
	if not Cooldown then
		Cooldown = true

		if NextAnim == 1 then
			NextAnim = 2
			PunchAnim:Play()
					wait(0.5)	
				
	
		elseif NextAnim == 2 then
			NextAnim = 3
			PunchAnim2:Play()
					wait(0.5)	
				
			
		elseif NextAnim == 3 then
			NextAnim = 1
			KickAnim:Play()
					wait(0.5)	
				end
		
		
		
		local CurrentNextAnim = NextAnim
		
		Cooldown = false
		wait(0.5)
		if CurrentNextAnim == NextAnim then 
			NextAnim = 1
		end
	end
end)

everything works fine, I just need to have ya know for the first hit the right arm, then for the second the left arm, and for the final attaack the Left leg to deal damage when it comes into contact with a playewr. I’ve been trying but i can’t seem to get anything to work
any help appreciated

1 Like

You would make a variable that stores whether the person can damage someone or not. Then, if the variable is true, you can fire a RemoteEvent which damages the victim. Here’s an example i made, I haven’t tested it yet:

--Put this script in StarterCharacterScripts:
local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Mouse = Player:GetMouse()
local PunchAnim = Humanoid:LoadAnimation(script:WaitForChild("Punch1Anim"))
local PunchAnim2 = Humanoid:LoadAnimation(script:WaitForChild("Punch2Anim"))
local KickAnim = Humanoid:LoadAnimation(script:WaitForChild("Kick1Anim"))
local RightArm = game.Players.LocalPlayer.Character["Right Arm"]
local LeftArm = game.Players.LocalPlayer.Character["Left Arm"]
local LeftLeg = game.Players.LocalPlayer.Character["Left Leg"]

local CanDamage = false
local Table = {RightArm, LeftArm, LeftLeg}
local NextAnim = 1 
local Cooldown = false

Mouse.Button1Down:Connect(function()
	if not Cooldown then
		Cooldown = true
		CanDamage = true

		if NextAnim == 1 then
			NextAnim = 2
			PunchAnim:Play()
					wait(0.5)	
				
	
		elseif NextAnim == 2 then
			NextAnim = 3
			PunchAnim2:Play()
					wait(0.5)	
				
			
		elseif NextAnim == 3 then
			NextAnim = 1
			KickAnim:Play()
					wait(0.5)	
				end
		
		
		
		local CurrentNextAnim = NextAnim
		
		Cooldown = false
		CanDamage = false
		wait(0.5)
		if CurrentNextAnim == NextAnim then 
			NextAnim = 1
		end
	end
end)

for i,v in pairs(Table) do
	v.Touched:Connect(function(h)
		if h.Parent:FindFirstChild("Humanoid") and CanDamage then
			CanDamage = false
			game.ReplicatedStorage.AttackEvent:FireServer(h.Parent) --Replace 10 with the amount of damage you want the victim to take!
		end
	end)
end
--And put this one in ServerScriptService:
local Remote = Instance.new("RemoteEvent")
Remote.Name = "AttackEvent"
Remote.Parent = game:GetService("ReplicatedStorage")

Remote.OnServerEvent:Connect(function(Player, Character)
	if Character:FindFirstChild("Humanoid") then
		Character.Humanoid:TakeDamage(10)
	end
end)
1 Like

You saved me yet again, thanks :slight_smile:

1 Like

Be sure to have the server do a sanity check, as exploits can happen if the client fires that event whenever they want (and clients certainly are able to do that).

For example, if the client tells the server to punch someone who is 10 studs away, something is wrong. If the client tells the server to do 10 punches in 1 second, even though the player should only be able to do 2 punches in a second (supposing), something is wrong.

1 Like

Interesting, how would I do this? I’m new to scripting. Could you show me an example

When your server receives an event from a player, assume that the client is lying to you. Instead of just doing what the client says, write code that will verify that everything appears okay.

This example shows the server checking if the player is too far away from another player

Remote.OnServerEvent:Connect(function(Player, Character)
	if Player and Player.Character and Character:FindFirstChild("Humanoid") then 
--I'm also checking that Player and Player.Character exist, not for security but for good coding
        if (Player.Character.Head.Position - Character.Head.Position).magnitude > 7 then
--uh oh, something is wrong, maybe kick the player or something        
    else
		Character.Humanoid:TakeDamage(10)
	end
end)

Also, keep track of how often the client punches someone so that they don’t punch more than they should be able to.

1 Like

Good morning. Why are we checking for their head position? lol

ohh nvm im guessing it was just an example lol, i get it ty