Ad
Log in to vote
1

teleporting players to another game loop?

Asked by 1 month ago
Edited 1 month ago

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

1 answer

Log in to vote
0
Answered by
sayer80 437 Moderation Voter
1 month ago

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)

0
Would appreciate if you accept when it helps sayer80 437 — 1mo
1
thanks! you helped me! OctavianH0310 25 — 1mo
1
actually, it stopped working now. it just doesnt teleport me anywhere OctavianH0310 25 — 1mo
0
Are there any errors? sayer80 437 — 1mo
0
When there is 1 error the loop can break forever sayer80 437 — 1mo
Ad

Answer this question