As stated in the comments, My code assumes you are checking that there are 3 players in the game. If there isn’t, It breaks. You can use this line of code to wait until there are a total of 3 players
repeat wait() until #Players:GetPlayers() >= 3
Insert it on line 9 so that any player team assigning isn’t done until 3 players are there.
[19:57:28.527 - ServerScriptService.gameManager:9: attempt to call method ‘GetPlayers’ (a nil value)]
19:57:28.529 - Stack Begin
[19:57:28.530 - Script ‘ServerScriptService.gameManager’, Line 9]
19:57:28.532 - Stack End
is now the error I get after adding this line of code.
while wait(10) do
local timeValue = game.ReplicatedStorage.timeValue
timeValue.Value = 5
local workspaceAudio = game.Workspace.workspaceAudio
game.Workspace.workspaceAudio.start:Play()
wait(5)
game.Workspace.workspaceAudio.start:Stop()
local Players = game.Players:GetPlayers()
repeat wait() until #Players:GetChildren() >= 3
local ap = Players -- Players that can be used; I'm using the players for now but you'll want to detect if other people are on a different team if you add multiple teams
-- You can do this with the TeamColor checking you have going on
local sp = {} -- Selected players
-- This code right here assumes that there is already 3 players, or you are checking to make sure that 3 players are there.
for i =1,3 do
local selected = math.random(1,#Players)
sp[i] = ap[selected];
ap[selected] = nil; --Make it so we cant choose them again
end
-- And then you have your 3 players
-- Now lets put those players in the right team
for _,plr in pairs(sp) do
plr.TeamColor = BrickColor.new("Bright blue")
end
end
[20:08:15.689 - ServerScriptService.gameManager:10: attempt to compare number with table]
20:08:15.693 - Stack Begin
[20:08:15.694 - Script ‘ServerScriptService.gameManager’, Line 10]
20:08:15.694 - Stack End
This is now the error I’m getting.
Now this error:
[20:12:48.842 - ServerScriptService.gameManager:17: bad argument #2 to ‘random’ (interval is empty)]
20:12:48.846 - Stack Begin
[20:12:48.847 - Script ‘ServerScriptService.gameManager’, Line 17]
20:12:48.848 - Stack End
What happens when you have only 3 players? If its 3 players on a team. do you only need one team? What if a player leaves midway through?
Also why are you using while wait() do? I get that you’re a beginner. But its still important to stick to a coding standard.
I recommend to take a step back and think about this system from an abstract point of view. See what functions you can use, the parameters those functions will need to achieve the same result without iteration, its totally possible. You can even hook up events to deal with situations where a player leave causing your game to be unbalanced.