Add a cooldown to teleport

I’m using this script that teleport you to a certain part, and I’m confused on how to add a cooldown to it.

Player = game.Players.LocalPlayer
To = game.Workspace.worthy
BlackScreen = script.Parent.BlackScreen
TeleportButton = script.Parent.TeleportButton
HasPressedTeleport = false

function MouseEnterTeleportButton()
	TeleportButton.FontSize = "Size28"
	TeleportButton.BackgroundColor3 = Color3.new(255, 1, 4)
	
end

function MouseLeftTeleportButton()
	TeleportButton.FontSize "Size24"
	TeleportButton.BackgroundColor3 = Color3.new(1, 1, 1)
	
end

function TeleportPlayer()
	if HasPressedTeleport == false then
		BlackScreen.Transparency = 1
		wait(0,1)
		BlackScreen.Transparency = 0.9
		wait(0,1)
		BlackScreen.Transparency = 0.8
		wait(0,1)
		BlackScreen.Transparency = 0.7
		wait(0,1)
		BlackScreen.Transparency = 0.6
		wait(0,1)
		BlackScreen.Transparency = 0.5
		wait(0,1)
		BlackScreen.Transparency = 0.4
		wait(0,1)
		BlackScreen.Transparency = 0.3
		wait(0,1)
		BlackScreen.Transparency = 0.2
		wait(0,1)
		BlackScreen.Transparency = 0.1
		wait(0,1)
		BlackScreen.Transparency = 0
		wait(0,1)
		Player.Character:MoveTo(To.Position)
		BlackScreen.Transparency = 0
		wait(0,1)
		BlackScreen.Transparency = 0.1
		wait(0,1)
		BlackScreen.Transparency = 0.2
		wait(0,1)
		BlackScreen.Transparency = 0.3
		wait(0,1)
		BlackScreen.Transparency = 0.4
		wait(0,1)
		BlackScreen.Transparency = 0.5
		wait(0,1)
		BlackScreen.Transparency = 0.6
		wait(0,1)
		BlackScreen.Transparency = 0.7
		wait(0,1)
		BlackScreen.Transparency = 0.8
		wait(0,1)
		BlackScreen.Transparency = 0.9
		wait(0,1)
		BlackScreen.Transparency = 1
		wait(0,1)
	end
end

TeleportButton.MouseEnter:connect(MouseEnterTeleportButton)
TeleportButton.MouseLeave:connect(MouseLeftTeleportButton)
TeleportButton.MouseButton1Down:connect(TeleportPlayer)
3 Likes

Since TeleportPlayer is what is called when the player makes the command, it’s in this function you’ll need to add the cooldown/debounce.

local debounce = false
function TeleportPlayer()
     -- First, check if we're on cooldown (debounce active).
     -- If it is, we use return to exit before doing anything.
     if debounce then return end
     -- At this point, we're off cooldown (debounce inactive).
     -- We go on cooldown by activating the debounce.
     debounce = true
     -- It's at this point your teleport is performed.
     -- I will omit this for brevity.
     -- ...
     -- ...
     -- Afterwards, go back off cooldown by deactivating the debounce.
     -- Make sure you don't return from your function before doing this,
     -- otherwise you'll stay on cooldown forever.
     debounce = false
end
1 Like

How would I put that into my script?

I already did. I just omit other parts because I didn’t want to re-type them.

would I make it similar to this?

local debounce = false
function TeleportPlayer()
	if HasPressedTeleport == false then
		BlackScreen.Transparency = 1
		wait(0,1)
		BlackScreen.Transparency = 0.9
		wait(0,1)
		BlackScreen.Transparency = 0.8
		wait(0,1)
		BlackScreen.Transparency = 0.7
		wait(0,1)
		BlackScreen.Transparency = 0.6
		wait(0,1)
		BlackScreen.Transparency = 0.5
		wait(0,1)
		BlackScreen.Transparency = 0.4
		wait(0,1)
		BlackScreen.Transparency = 0.3
		wait(0,1)
		BlackScreen.Transparency = 0.2
		wait(0,1)
		BlackScreen.Transparency = 0.1
		wait(0,1)
		BlackScreen.Transparency = 0
		wait(0,1)
		Player.Character:MoveTo(To.Position)
	if debounce then return end
	-- At this point, we're off cooldown (debounce inactive).
	-- We go on cooldown by activating the debounce.
	debounce = true
		BlackScreen.Transparency = 0
		wait(0,1)
		BlackScreen.Transparency = 0.1
		wait(0,1)
		BlackScreen.Transparency = 0.2
		wait(0,1)
		BlackScreen.Transparency = 0.3
		wait(0,1)
		BlackScreen.Transparency = 0.4
		wait(0,1)
		BlackScreen.Transparency = 0.5
		wait(0,1)
		BlackScreen.Transparency = 0.6
		wait(0,1)
		BlackScreen.Transparency = 0.7
		wait(0,1)
		BlackScreen.Transparency = 0.8
		wait(0,1)
		BlackScreen.Transparency = 0.9
		wait(0,1)
		BlackScreen.Transparency = 1
		wait(0,1)
	debounce = false
end

can you also help me with the error

No, you’d need to surround everything in your teleport operation with the debounce check and set. As in, before fading out, you need to have checked and set the debounce. After fading back in is when you’d unset the debounce.

I can’t help you with an error you haven’t posted about.

this is part of what I have but the debounce isn’t working

local debounce = false
function TeleportPlayer()
	if HasPressedTeleport == false then
		BlackScreen.Transparency = 1
		wait(0,1)
		BlackScreen.Transparency = 0.9
		wait(0,1)
		BlackScreen.Transparency = 0.8
		wait(0,1)
		BlackScreen.Transparency = 0.7
		wait(0,1)
		BlackScreen.Transparency = 0.6
		wait(0,1)
		BlackScreen.Transparency = 0.5
		wait(0,1)
		BlackScreen.Transparency = 0.4
		wait(0,1)
		BlackScreen.Transparency = 0.3
		wait(0,1)
		BlackScreen.Transparency = 0.2
		wait(0,1)
		BlackScreen.Transparency = 0.1
		wait(0,1)
		BlackScreen.Transparency = 0
		wait(0,1)
		Player.Character:MoveTo(To.Position)
		BlackScreen.Transparency = 0
		wait(0,1)
		BlackScreen.Transparency = 0.1
		wait(0,1)
		BlackScreen.Transparency = 0.2
		wait(0,1)
		BlackScreen.Transparency = 0.3
		wait(0,1)
		BlackScreen.Transparency = 0.4
		wait(0,1)
		BlackScreen.Transparency = 0.5
		wait(0,1)
		BlackScreen.Transparency = 0.6
		wait(0,1)
		BlackScreen.Transparency = 0.7
		wait(0,1)
		BlackScreen.Transparency = 0.8
		wait(0,1)
		BlackScreen.Transparency = 0.9
		wait(0,1)
		BlackScreen.Transparency = 1
		wait(0,1)
	if debounce then return end
-- At this point, we're off cooldown (debounce inactive).
	-- We go on cooldown by activating the debounce.
		debounce = true
		
	debounce = false

Something tells me you’re not using the Output window to check errors because wait(0,1) ought to be wait(0.1). Perhaps you should read Debugging Tools on the devhub.

wait (0,1) also works and there is one error, image

wait(0,1) is correct syntax but is not correct semantically. It is the same as wait(0), which doesn’t do what you think it does (it waits 1/30th of a second, about 0.03s, or close to it). A comma cannot be used in place of a period when specifying numbers. Here, it is permissible because it is interpret as sending two parameters, 0 and 1, to wait. Wait only takes 0 or 1 parameters.

Back on topic…

Your original code has the following line:

TeleportButton.FontSize "Size24"

You probably meant this to be an assignment of the FontSize property of TeleportButton. Due to the missing =, Lua thinks you want to call a FontSize function with “Size24” as the only argument (that’s where the error comes from). The property is also deprecated, and you should be using TextSize instead.

TeleportButton.TextSize = 24

I think your problem is when your setting the font size setting, your using the enum instead of the property value. Look Here for a table of font sizes. The first colum is for enums, the second is for setting the properties.