Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Problem with my player choosing script?

Asked by
Relatch 550 Moderation Voter
9 years ago
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

Log in to vote
1
Answered by 9 years ago

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

Answer this question