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

I want to add a cooldown to this teleport script but wait() doesn't work any help?

local tool = script.Parent
local plr = game:GetService('Players').LocalPlayer
local mouse = plr:GetMouse()

local function Tele()
	tool.Parent:moveTo(mouse.hit.Position)

end

tool.Activated:Connect(Tele)

2 comments

0

Question, what's a

?

0

Oh I accidentally typed that here the new one

2 answers

✓ Accepted answer

One way of adding a cooldown would be to use a variable, and set it to true once the action is being completed and then set it to false after a set amount of time has passed. At the start of the function, you'll want to make sure that the variable is false.

local tool = script.Parent
local plr = game:GetService('Players').LocalPlayer
local mouse = plr:GetMouse()
local cooldown = 5--How many seconds you want the cooldown to be
local db=false
local function Tele()
	if db==false then
	db=true
    tool.Parent:moveTo(mouse.hit.Position)
    wait(cooldown)
    db=false
end

tool.Activated:Connect(Tele)

In this example, once the function runs, it makes db=true and then after 5 seconds, db=false. If we check for db to be equal to false at the start, this means that for 5 seconds, the function will not be able to run

3 comments

0

wow it works THANK YOU GUYS

0

Of course, no problem :)

0

set this question as answered

You can add a debounce for each time you teleported.

local tool = script.Parent
local plr = game:GetService('Players').LocalPlayer
local mouse = plr:GetMouse()

local deb = false
local cooldown = 5

local function Tele()
	if deb then return end -- if deb is true then stop this function and continue
	
	tool.Parent:moveTo(mouse.hit.Position)
	deb = true
	wait(cooldown)
	deb = false
end

tool.Activated:Connect(Tele)

1 comment

0

It works thank you

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