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

How would I add a cooldown to this textbutton script?

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

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)

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