Error trying to put 3 players on a team

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.

1 Like

I had 3 players in a local server when this error occured.

Still add the check, As the script runs before the players join in

1 Like

Will do this now, wish me luck.

1 Like

[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.

Replace GetPlayers()
with GetChildren()

1 Like

[20:01:52.391 - ServerScriptService.gameManager:9: attempt to call method ‘GetChildren’ (a nil value)]

Can you post that line and the ones above it? Something else is wrong here

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

Oh im just dumb.
I usually tie Players to the service

game.Players:GetChildren()

Replace the bit after “until” with that

1 Like

It’s fine. I’m testing the new code now. Thanks!

1 Like

[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.

put a # infront of the thing you just added. Hopefully this will work for the fifth time

1 Like

Good counting, even though I didn’t fact check this “fifth time” shenanigan. Oh well, testing out the code now.

1 Like

the wonders of troubleshooting is you dont even know how many times you’ve done it before it takes over your mind

1 Like

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

Avoid polling if you can, when waiting for a minimum number of players you can just do the following:

local MIN_PLAYERS = 3
local Players = game:GetService("Players")

while #Players:GetPlayers() < MIN_PLAYERS do
	Players.PlayerAdded:Wait()
end

Constantly looping with wait() is rarely the best solution.

What happens when you have only 3 players? If its 3 players on a team. :thinking: 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.

Hey another issue, Know the right thing this time

On line 8 you got the players, all nice and everything until you realise it doesnt update

Add another line and with the new line at line 15, add this

Players = game.Players:GetPlayers()

This should hopefully work now

1 Like

It works now. Thanks a lot! :smiley:

1 Like