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

How do I make this script only run after a certain amount of time since the player last used it?

I have a script that teleports a player once they step on it. The problem is that once they step on it once they get stuck in an infinite loop as it keeps on teleporting them back and for. I need to create a wait time so that it only inputs the code every 10 seconds or so. I've been trying to figure out how to do this with debounce but I am new to scripting and I am quite confused.

local Teleport = "Teleporter" 
function Touch(hit)
	wait (5)
	if script.Parent.Locked == false and script.Parent.Parent:findFirstChild(Teleport).Locked == false then script.Parent.Locked = true script.Parent.Parent:findFirstChild(Teleport).Locked = true 

	local Pos = script.Parent.Parent:findFirstChild(Teleport).
		hit.Parent:moveTo(Pos.Position) wait(1) script.Parent.Locked = false script.Parent.Parent:findFirstChild(Teleport).Locked = false end end

script.Parent.Touched:connect(Touch)

2 comments

0

Do you mean so you cant spam the script over and over again?

0

Yes that is what I mean. Once I teleport up to the top teleporter it starts the script again and just teleports me back down

1 answer

local Teleport = "Teleporter" 
local debounce = false
local time = 1 -- change to change cooldown
function Touch(hit)
    wait (5)
if debounce == false then
    debounce = true
    local Pos = script.Parent.Parent:findFirstChild(Teleport).
    hit.Parent:moveTo(Pos.Position)
    wait(time)
    debounce = false
 end 
end

script.Parent.Touched:connect(Touch)

1 comment

0

This one doesn't seem to work. I teleport up and do things but after a certain amount of time it just teleports me down again.

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