I am trying to make a teleporter that teleports the players on it every 23 seconds but it doesn't work properly. It waits 23 seconds before teleporting for the first time but after it will teleport players all the time. Can somebody help me please? ( i am kinda new to scripting) This is my code:
local TeleportService = game:GetService("TeleportService") local gameID = 6446611098 -- Game ID while true do wait(23) function onTouched(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) if player then TeleportService:Teleport(gameID, player) end end script.Parent.Touched:connect(onTouched) end
So the problem is the script first runs (wait 23 seconds) then the Function is active and listening and whenever a player touches the part they will get tped and the Function should also stack after each 23 seconds like it will try to teleport them to there 2 Times which is inefficient.
The Explanation is inside the code.
local TeleportService = game:GetService("TeleportService") local gameID = 6446611098 -- Game ID local waitingtime = 23 -- time to wait local queue = {} -- stores player information local function statushandler(player) for _, queueplayer in pairs(queue) do if queueplayer == player then return end -- stops function when player is already in queue end table.insert(queue,player) -- add player to the queue end spawn(function() -- use spawn so the other parts of the code wont be interrupted while true do queue = {} -- resets queue ( table ) wait(waitingtime) for _, player in pairs(queue) do -- gets a list of everything in queue local plr = game:GetService("Players"):FindFirstChild(player) -- gets player if plr then -- checks if player exists TeleportService:Teleport(gameID, player) -- teleports end end end end) local function onTouched(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) if player then -- check if there is a player statushandler(player.Name) -- sends playername end end script.Parent.Touched:Connect(onTouched)