Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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

0
I've tried it but it still doesn't stop it from attacking when spamming E. Thanks for the effort tho! ANormalPlayer_Alex 11 — 4y
Ad

Answer this question