What do you want to achieve? Keep it simple and clear!
I want to put 3 random players on a team named “Blue”.
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)
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
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
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.