Error trying to put 3 players on a team

  1. What do you want to achieve? Keep it simple and clear!
    I want to put 3 random players on a team named “Blue”.

  2. What is the issue? Include screenshots/videos if possible!
    I get an error whenever trying to do so. The error is:
    ServerScriptService.gameManager:10: bad argument #2 to ‘random’ (interval is empty)

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried:
    Using different solutions on other websites
    Tried fixing it myself

Script: https://pastebin.com/raw/kE30KnDi
or:

Cancel that, This code won’t work at all if you want 3 players on a team, I’ll edit with an example in a moment

1 Like

Use this:

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()
local SelectedPlayer1 = math.random(1,#Players)
local SelectedPlayer2 = math.random(1,#Players)
local SelectedPlayer3 = math.random(1,#Players)
for i = 1,#Players do
if i == SelectedPlayer1 or i == SelectedPlayer2 or i == SelectedPlayer3 then
bluePlayers.TeamColor = BrickColor.new("Bright blue")
end
end
1 Like

https://pastebin.com/raw/kCBcVZnH

I personally prefer using this, because then you can add even more players and it teaches against redundant code, In my opinion @OldBo5

1 Like

I get this error with your script:
[19:41:47.772 - ServerScriptService.gameManager:16: bad argument #2 to ‘random’ (interval is empty)]

19:41:47.995 - Stack Begin

[19:41:47.996 - Script ‘ServerScriptService.gameManager’, Line 16]
19:41:47.997 - Stack End

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