Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is the damage and cooldown not working in my combat script?

I created a combat script that has an animation. The animation works in the script, but I tried making it do damage and I also tried making a cooldown, but neither of those work. I'm able to spam the key instead of waiting before I can use it again. The damage also doesn't work because if I press the key and the animation isn't active, but I touch a humanoid it loses health. I just need help figuring out what I did wrong with the cooldown and the damage. This is the script:

game.ReplicatedStorage.Chop.OnServerEvent:Connect(function(plr)
	local rArm = plr.Character["Right Arm"]
	local debounce = false
	local hum = plr.Character["Humanoid"]
	local anim = Instance.new("Animation", workspace)
	anim.AnimationId = "rbxassetid://5821099556"
	local animplay = hum:LoadAnimation(anim)
	animplay:Play()
	rArm.Touched:Connect(function(hit)
		if not debounce then
			debounce = true
		if hit.Parent:FindFirstChild("Humanoid") then
			hit.Parent.Humanoid:TakeDamage(45)
				wait(5)
				debounce = false
			end
		end
	end)
end)

5 comments

0

You have to put the local debounce at the very start of the script. Not inside the function.

0

When you don't have your tool out, it would still do damage through the arm because the event is still registered.

0

Ok I'll try that.

0

I moved local debounce at the start of the script, but I'm still able to spam the animation.

1 answer

✓ Accepted answer

Alright so I messed up the first time but I think this should work now. (explanations in script)

local debounce = false -- debounce is outside the script
local debounce2 = false -- 2nd debounce for damage
local anim = Instance.new("Animation", workspace) -- animation is outside so you don't make a new instance every time you click
anim.AnimationId = "rbxassetid://5821099556" -- animationID

game.ReplicatedStorage.Chop.OnServerEvent:Connect(function(plr) -- on serverevent
	local rArm = plr.Character:WaitForChild("Right Arm") -- waiting for right arm
	local hum = plr.Character:WaitForChild("Humanoid") -- waiting for humanoid
	local animplay = hum:LoadAnimation(anim)
	if not debounce then -- if debounce == false then
		debounce = true
		debounce2 = false -- debounce2 is now false so damage can be used
		animplay:Play() -- playing animation
		wait(5) -- make sure this wait is how much time it will take for your animation to end
		debounce = false -- debounce = false
	end
	rArm.Touched:Connect(function(hit) -- when arm hits
		if not debounce2 and debounce then -- checking if animation is playing and if you're able to damage
			debounce2 = true -- now you can't damage anymore
			if hit.Parent:FindFirstChild("Humanoid") then -- checking if what you hit is a humanoid
				hit.Parent.Humanoid:TakeDamage(45) -- makes them take damage
			end
		end
	end)
end)

1 comment

0

Thanks it works.

This discussion is preserved as it was. New answers, comments, and votes are closed.