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

How would I add a cooldown to this textbutton script?

Asked by 5 years ago

How would I add a cooldown to this textbutton script, so that people won't be able to spam it and glitch the button?

local original = script.Parent.Text

script.Parent.MouseButton1Click:Connect(function()
    local player = game.Players.LocalPlayer
        if player.leaderstats.Points.Value >= 50 then    
            print("found leaderstats and firing server")
            game.ReplicatedStorage.TeleportEvent:FireServer(player)
            script.Parent.Text = "Teleported!"
            wait(1)
            script.Parent.Text = original
    else
        script.Parent.Text = "Not enough Points"
        wait(2)
        script.Parent.Text = original
    end
end)

1 answer

Log in to vote
1
Answered by 5 years ago
local original = script.Parent.Text

-- it is common to create a module to make a debounce 
local function addDeb(f)
    local deb = true
    return function(...)
        if deb then return end
        deb = true
        f(...) -- run function
        deb = false
    end
end


local function code()
    local player = game.Players.LocalPlayer
        if player.leaderstats.Points.Value >= 50 then    
            print("found leaderstats and firing server")
            game.ReplicatedStorage.TeleportEvent:FireServer(player)
            script.Parent.Text = "Teleported!"
            wait(1)
            script.Parent.Text = original
    else
        script.Parent.Text = "Not enough Points"
        wait(2)
        script.Parent.Text = original
    end
end

local debRun = addDeb(code)
script.Parent.MouseButton1Click:Connect(debRun)
Ad

Answer this question