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

Why does it not teleport the players to the lobby when the round finishes?

Asked by 5 years ago

What is the problem

The problem is pretty much stated above. The script does not teleport the players to the lobby.

Weird Results

Before I even go inside the part, ExitRegion, it prints "MAX_TIME reached even though it hasn't reached max time.

Any print inside of the final loop isn't printing

Code

NOTE: This is part of a bigger script.

--\\ Services
local players = game:GetService("Players")
local repStorage = game:GetService("ReplicatedStorage")
--\\ Exit Player
local exitRegion = game.Workspace.Map:WaitForChild("ExitRegion")
local db = false

local lobbySpawn = game.Workspace:WaitForChild("Spawn")
local exitRemote = repStorage:WaitForChild("ExitRemoteEvent")
local teleportRemote = repStorage:WaitForChild("TeleportPlayersRemoteEvent")

local MAX_TIME = 120

players.PlayerAdded:Wait()
while wait() do

    local inRound = {}
    local finished = {}
    local currentTime = 0

    for i = 1,#players:GetPlayers() do
        inRound[#inRound + 1] = players:GetChildren()[i]
    end

    exitRegion.Touched:Connect(function(hit)
        if hit.Parent:FindFirstChild("Humanoid") and not db then
            db = true
            local player = players:GetPlayerFromCharacter(hit.Parent)   
            --\\ Finish Player
            if player then
                print(player.Name .. " isn't nil")
                for index,finisher in pairs(inRound) do
                    if finisher.Name == player.Name and not finished[index] then 
                        print(finisher.Name.. " has been inserted in the finish array")
                        finished[#finished + 1] = finisher
                        print(player.Name)
                        exitRemote:FireClient(player,exitRegion)
                        finisher = nil
                        break
                    else
                        print(finisher.Name .. " is not " .. player.Name)
                    end 
                end 
            end
            wait(1)
            db = false
        end
    end)
    coroutine.wrap(function()
        while currentTime < MAX_TIME and not #inRound == 0 do
            wait(1)
            currentTime = currentTime + 1
            print(currentTime)
        end
        print("MAX_TIME has reached")
        inRound = {}
    end)()
    coroutine.wrap(function()
        repeat wait() until #inRound == 0
    end)()
    --\\ Teleport player back
    for _,winner in pairs(finished) do
        print("Looping finished players")
        local char = winner.Character
        if char then
            print("Character's HumanoidRootPart is not nil!")
            local torso = char:WaitForChild("HumanoidRootPart")
            torso.CFrame = CFrame.new(lobbySpawn.Position)
            print(winner .. " has been inserted in the finished array")
        else
            print("The Character's HumanoidRootPart is nil!")
        end
    end
end

Answer this question