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

How to stop a script from damaging the Humanoid when spamming E?

I don't know how to explain in such few words, but basically what my script does is that whenever the player presses E and a humanoid is touching the Handle of the tool, it damages him, the problem is that you can just spam E and damage the Humanoid alot while still in the attack animation, does anyone know how to fix it?

--\ The Script //--

--\\ VARIABLES //--
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local swing = Enum.KeyCode.E
local spin = Enum.KeyCode.Q
local damage = 0

--\\ SWING //--

function swing(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.E then
		damage = 15
		script.Parent.Touched:Connect(function(part)
			wait(.7)
			if part.Parent:WaitForChild("Humanoid") then
				part.Parent.Humanoid:TakeDamage(damage)
				damage = 0
			end
		end)
	end
	end

UserInputService.InputBegan:Connect(swing)


Any help?

1 answer

So, what you need to do is add a debounce to your script. You seemingly did add a variable named debounce, but perhaps you forgot to actually code the debounce in? This code should be working:

--\\ VARIABLES //--
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local swing = Enum.KeyCode.E
local spin = Enum.KeyCode.Q
local damage = 0
local debounce = false

--\\ SWING //--
function swing(inputObject, gameProcessedEvent)
	if debounce == false then
		debounce = true
		if inputObject.KeyCode == Enum.KeyCode.E then
			damage = 15
			script.Parent.Touched:Connect(function(part)
				wait(.7)
				if part.Parent:WaitForChild("Humanoid") then
					part.Parent.Humanoid:TakeDamage(damage)
					damage = 0
				end
			end)
		end 
	end
	debounce = false
end

UserInputService.InputBegan:Connect(swing)

If you want to read about debounces and when to use them, check out this article. https://developer.roblox.com/en-us/articles/Debounce

1 comment

0

I've tried it but it still doesn't stop it from attacking when spamming E. Thanks for the effort tho!

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