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

math.random error. Please help?

Asked by 9 years ago

I get this error on my main game script: 17:36:39.401 - ServerScriptService.MainGameScript:26: bad argument #2 to 'random' (interval is empty)

This is line 26: d1 = players[math.random(1, #players)]

Yes, players is defined. Please help? Confused. Also, I know that the second argument cannot be lower than the first one, but the second argument is always wrong even if the table is reading it, let's say 1-3.

0
Please show us how [players] is defined in the script. magiccube3 115 — 9y
0
local players = game.Players:GetPlayers() passtimed 45 — 9y

2 answers

Log in to vote
0
Answered by 9 years ago

Make sure you're using the typical events to detect players being added to your game, before calling this - otherwise you won't get the correct results.

game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(character)


    -- Your code goes here 
    local players = game.Players:GetPlayers()
    --[[
         this is for test only , but you would put the code below in a function that picks the random player before starting  a new round
    --]]    

    d1 = players[math.random(1, #players)]

end)
end)

Ad
Log in to vote
0
Answered by
Validark 1580 Snack Break Moderation Voter
9 years ago

Please make sure players is defined as such:

local players = game.Players:GetPlayers()

Answer this question