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

How do I add debounce to this script so it doesn't reptitvaly trigger?

How do I add debounce to this script so it doesn't reptitvaly trigger? I never learned debounce.

script.Parent.Touched:connect(function(hit)
	h = hit.Parent:FindFirstChild("Humanoid")
	if (h ~= nil) then
		m = Instance.new("Message")
		m.Text = "Press 'V' to climb the ladder"
		m.Parent = Workspace
	end
end)

3 answers

✓ Accepted answer
local debounce = false 
local timeBetween = 5 --The time in seconds that the message will stay for.
script.Parent.Touched:connect(function(hit)
    if debounce then --If the debounce is enabled
        return nil --End the function
    end
    debounce = true --Enable the debounce
    h = Game.Players:GetPlayerFromCharacter(hit.Parent) --Get the player.
    if (h ~= nil) then --If the part that touched was part of a Player then
        m = Instance.new("Message", Workspace) --Make a message (should probably be in Player's PlayerGui)
        m.Text = "Press 'V' to climb the ladder"
	wait(timeBetween) --Wait for the length of the "timeBetween" variable
	debounce = false --Disable the debounce
	m:Destroy() --Destroy the message
    end
end)

1 comment

0

Worked.

local db = false

script.Parent.Touched:connect(function(hit)
	if db then return end
	if hit.Parent:FindFirstChild("Humanoid") then
		local m = Instance.new("Message", Workspace)
		m.Text = "Press 'V' to climb the ladder"
		db = true
		wait(3)
		db = false
	end
end)

debounce = true
script.Parent.Touched:connect(function(hit)
    h = hit.Parent:FindFirstChild("Humanoid")
    if (h ~= nil) then
        m = Instance.new("Message")
        m.Text = "Press 'V' to climb the ladder"
        m.Parent = Workspace
    debounce = false
wait(5)
debounce = true
	end
end)

Forgive me if I'm wrong, I'm not that good at this.

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