Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Problem with my player choosing script?

local gui = game.StarterGui

local serverstorage = game:GetService("ServerStorage")

local agentsword = serverstorage:WaitForChild("AgentSword")
local assassainsword = serverstorage:WaitForChild("AssassainSword")
local bystandersword = serverstorage:WaitForChild("BystanderSword")

alive = {}
for _, player in pairs(game.Players:GetPlayers()) do
    table.insert(alive, player)
end

agent = alive[math.random(1, #alive)]
humanoid = agent:WaitForChild("Humanoid")

agent:MoveTo(game.Workspace.AgentSpawn.Position)

Error: ServerScriptService.MainScript:14: bad argument #2 to 'random' (interval is empty)

1 answer

✓ Accepted answer

The script runs before there are any players in the server, so #alive will equal 0 and the math.random function will break the script.

Add this before the for loop on line 10:

repeat wait() until game.Players.NumPlayers > 0 --You can change this if you like so that the agent is chosen when a certain number of players enter the game.

This discussion is preserved as it was. New answers, comments, and votes are closed.