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

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

Asked by 10 years ago

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

Log in to vote
0
Answered by 10 years ago
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)

0
Worked. BosswalrusTheCoder 88 — 10y
Ad
Log in to vote
-1
Answered by 10 years ago
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)

Log in to vote
-2
Answered by 10 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.
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.

Answer this question