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

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

Asked by 2 years ago
Edited 2 years ago
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)

0
Question, what's a <pre class>? NotThatFamouss 605 — 2y
0
Oh I accidentally typed that here the new one Teerachor 2 — 2y

2 answers

Log in to vote
0
Answered by 2 years ago

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

0
wow it works THANK YOU GUYS Teerachor 2 — 2y
0
Of course, no problem :) boqueburro 57 — 2y
0
set this question as answered TheB4dComputer 100 — 2y
Ad
Log in to vote
1
Answered by 2 years ago

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)

0
It works thank you Teerachor 2 — 2y

Answer this question