Hello yet again!

I am creating a silly little brawling game that involves punching and stomping your enemies - think "The Streets 2" with less "realism" and more memes.

I set up a weird string of code to allow the player to punch, or stomp, if they press a button. If the punch lands, whatever is hit takes damage. The problem is, the script does not end when needed, and the players arm or leg will constantly damage whatever it touches, even after the code is supposed to end. I'm still learning Lua so my methods are sloppy and probably not the best way to go about it.

This is the code:

-- locals, again

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local punchdamageEvent = ReplicatedStorage.Events:WaitForChild("PunchDamageEvent")
local stompdamageEvent = ReplicatedStorage.Events:WaitForChild("StompDamageEvent")
local punchAnimScript = game:GetService("StarterGui"):WaitForChild("punch")
local stompAnimScript = game:GetService("StarterGui"):WaitForChild("stomp")

dmg = 20
CanDmg = true
minimumhealth = 20

-- punchy lad
function onPunchDamageFired(Player)
	Player.Character:WaitForChild("Right Arm").Touched:Connect(function(hit)
		if hit.Parent.Humanoid and CanDmg == true and hit.Parent.Humanoid.Health > minimumhealth then
			hit.Parent.Humanoid:TakeDamage(dmg)
			CanDmg = false
			wait(1.5)
			CanDmg = true
			print 'punch succeeded'
		end
	end)
end

punchdamageEvent.OnServerEvent:Connect(onPunchDamageFired)


-- stompy lad
function onStompDamageFired(Player)
	Player.Character:WaitForChild("Right Leg").Touched:Connect(function(hit)
		if hit.Parent.Humanoid and CanDmg == true then
			hit.Parent.Humanoid:TakeDamage(dmg)
			CanDmg = false
			wait(1.5)
			CanDmg = true
			print 'kick succeeded'
		end
	end)
end

stompdamageEvent.OnServerEvent:Connect(onStompDamageFired)

The animations are triggered separately through a LocalScript, placed in the PlayerGui. Upon pressing one of the two keybinds, a RemoteEvent is fired and the script above runs. Everything works as intended, I just can't figure out how to stop the script from properly running, and preventing further unneeded damage from occurring!

2 comments

0

Maybe try putting the debounce above the damage

0

Look up how to :Disconnect() functions. Do this once the touched function fires.