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

Damage Script skips the wait while dealing damage?

I want the script to damage the player on touch every five seconds, but it damages the player repeatedly without waiting one second.

function onTouch(part) 
local human = part.Parent:findFirstChild("Humanoid") 
if (human == nil) then return end -- if it is not a humanoid, then do nothing, otherwise...
human.Health = human.Health - 5 -- then kill the humanoid
wait(1)
end 
script.Parent.Touched:connect(onTouch)


2 comments

0

Use debouce(if else).

0

Event performs the function without waiting for the end of the function. You must create debounce variable and specify " if else"

1 answer

###Use a debounce. You can delay code with them, or completely stop code.

local debounce = false

script.Parent.Touched:Connect(function(part)
    -- :Connect not :connect, :connect is deprecated 
    local human = part.Parent:FindFirstChild("Humanoid") 
        -- :FindFirstChild not :findFirstChild, :findFirstChild is deprecated
    if human and not debounce then -- no need for ~= nil or the brackets
        debounce = true -- set to true if humanoid is present 
        wait(5)
        debounce = false -- set back to false so code can run again
    end
end)

####.

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