Hello! I am currently working on a game intermission and in game system where it picks a random person to become what I will call “green” and the rest to become “employees” but I am having a weird glitch where sometimes everyone becomes the “green” or one person become the “green”. I can’t figure out why and I was wondering if I could get some help!
local ServerStorage = game:GetService("ServerStorage")
local CoreValues = ServerStorage.CoreValues
local Timer = CoreValues.TimeRemaining
local gameStatus = game.ServerStorage.CoreValues.GameStatus
local greenteam = game.Teams.GreenScreen
local lobbyteam = game.Teams.Lobby
local employees = game.Teams.Employees
repeat wait() until game.Players.NumPlayers >= 2
while true do
gameStatus.Value = "Intermission"
for index, player in pairs(game.Players:GetPlayers()) do
local character = player.Character or player.CharacterAdded:Wait()
character.HumanoidRootPart.CFrame = workspace.LobbySpawn.CFrame
player:LoadCharacter()
player.Team = lobbyteam
end
for i=15, 0, -1 do
Timer.Value = i
wait(1)
end
gameStatus.Value = "Time Left"
for index, player in pairs(game.Players:GetPlayers()) do
local players = game.Players:GetPlayers()
local nplr = #players
local greenscreen = players[math.random(1, nplr)]
local character = player.Character or player.CharacterAdded:Wait()
local clone = game.ReplicatedStorage.Character.Green:Clone()
character.HumanoidRootPart.CFrame = workspace.MapSpawn.CFrame
player.Team = employees
greenscreen.Character = clone
clone.Parent = workspace
greenscreen.Team = greenteam
end
for i=30, 0, -1 do
Timer.Value = i
wait(1)
end
end
I don’t know what’s wrong but I think it has something to do with the randomizer script. Thanks if you can help!
Can you try to print the amount of players they are in this game (basically printing the nplr variable’s value) and also the player in i,p in pairs is already defined as game.Players:GetPlayers()
game.Players.NumPlayers is still a thing it’s just that no one really uses it these days since that feature is depecrated (but like i said you can still use it)
local rng = Random.new() -- basically just math.random but better
local players = game.Players:GetPlayers() -- creates array of players so we can easily remove green to make them not chosen twice
local greenIndex = rng:NextInteger(1, #players) -- selects the green players index, you may need the index if you're using table.remove()
local green = players[greenIndex]
for _, player in ipairs(players) do -- ipairs is basically just pairs but for one player
if player ~= green then
-- do the stuff for the employees
else
-- for green
end
end
local ServerStorage = game:GetService("ServerStorage")
local CoreValues = ServerStorage.CoreValues
local Timer = CoreValues.TimeRemaining
local gameStatus = game.ServerStorage.CoreValues.GameStatus
local greenteam = game.Teams.GreenScreen
local lobbyteam = game.Teams.Lobby
local employees = game.Teams.Employees
repeat wait() until game.Players.NumPlayers >= 2
while true do
gameStatus.Value = "Intermission"
for index, player in pairs(game.Players:GetChildren()) do
local character = player.Character or player.CharacterAdded:Wait()
character.HumanoidRootPart.CFrame = workspace.LobbySpawn.CFrame
player:LoadCharacter()
player.Team = lobbyteam
end
for i=15, 0, -1 do
Timer.Value = i
wait(1)
end
gameStatus.Value = "Time Left"
local players = game.Players:GetChildren()
local nplr = #players
local greenscreen = players[math.random(1, nplr)]
local character = player.Character or player.CharacterAdded:Wait()
local clone = game.ReplicatedStorage.Character.Green:Clone()
character.HumanoidRootPart.CFrame = workspace.MapSpawn.CFrame
player.Team = employees
greenscreen.Character = clone
clone.Parent = workspace
greenscreen.Team = greenteam
for i=30, 0, -1 do
Timer.Value = i
wait(1)
end
end