0
votes

I have a script working for a rounds based game. It starts with a lobby and an intermission timer of 30 seconds. Then teleports players into a chosen map within a region I defined. During the round it counts down and either ends the round after 180 seconds or if 1 player is left. Players who are killed (or remain at the end of the 180 seconds) are teleported back to the lobby and the cycle continues. I used the script below and it works well. I would like to leverage the table / array from this script to identify players who are in the region and turn off / make invisible a Spectate GUI that I would like to make visible only to players in the lobby (ie. NOT in the region I defined that encompasses the various round maps). But I'm having trouble figuring out how to amend this script. Any help would be greatly appreciated! thanks. CODE below:

'''

while true do -- repeats forever and calls the functions

wait(2)
intermissionTimer()

chooseMap()
loadMap()
wait(2)
teleportPlayers()
wait(2)

local time = roundLength

while wait(1) do
    
    partsInRegion = workspace:FindPartsInRegion3(region, game.ServerStorage.Maps,3000) -- this returns a table 
    playersFound = {} -- table players found

    for i, part in pairs (partsInRegion) do
        if part.Parent:FindFirstChild("Humanoid") ~= nil then
            playersFound[part.Parent.Name] = part.Parent -- add players character to table
            winner = playersFound[part.Parent.Name].Name
            print (winner)
            print (i, part) -- 0
            
        end 
    end
    
    function Length(playersFound)
        local counter = 0 
        for _, v in pairs(playersFound) do
            counter =counter + 1
        end
        return counter
    end
    

    if time == 0 then
        Status.Value = "Round over!"
        workspace.SoundLibrary.Nowinner:Play()
        break
    elseif Length(playersFound)== 1 then
        
        workspace.SoundLibrary.Winner:Play()
        Status.Value = winner.. " wins!"

        wait(5)
        break
    else
        Status.Value = time .." seconds left"
        time = time - 1
    end 
end 

wait (2)
teleportBack()  
deleteMap()

end

'''

1

1 Answers

0
votes

At the end of your for-loop, you have a table full of players who are still in the game. You can get a list of all of the players in the game by using the Players service. Each player object is stored as a child there, and you can compare the names of your surviving players against the names of all the players to find who is dead.

local partsInRegion = workspace:FindPartsInRegion3(region, game.ServerStorage.Maps,3000)
local playersFound = {}

-- find all the players still alive
for k, part in pairs(partsInRegion) do
    if part.Parent:FindFirstChild("Humanoid") ~= nil then
        playersFound[part.Parent.Name] = part.Parent
    end 
end

-- figure out who is dead
local allPlayers = game.Players:GetChildren()
for i, player in ipairs(allPlayers) do
    -- if they are not still in the game, they must be dead
    if playersFound[player.Name] == nil then
        -- get the Spectate gui, replace this line with your actual gui
        local gui = ReplicatedStorage.SpectateGui:Clone()

        -- give the dead player the spectate gui, if they don't already have it
        if player.PlayerGui[gui.Name] == nil then
            gui.Parent = player.PlayerGui
        end
    end
end