ServerScriptService.Script:5: invalid argument #2 to 'random' (interval is empty)

So i am a beginner scripter and i wanted to make something simple where you print a random players name.

I got this error:

Here is the code im having problems with:

local player = game.Players:GetChildren()

wait(5)

local Rp = player[math.random(1,#player)]
print(Rp)

local players = game.Players:GetChildren()
wait(5)
local RandomPlayer = players[math.random(1, #players)]
print(RandomPlayer.Name)

This should work, but make sure its being run in Play Solo mode with at least one player in the actual game.

“interval is empty” means there’s no range. e.g math.random(1, 0). doesn’t make sense, right?
in that case, lets make your script wait until there is atlease 1 player in the server.

local Players = game:GetService("Players")

function PlayerAdded(Player) -- "Player" isn't needed in this case. we're just using the playeradded event.
    local players = Players:GetPlayers()
    local Rp = players[math.random(1,#players)]
    print(Rp)
end

for _, Player in next, Players:GetPlayers() do PlayerAdded(Player) end
Players.PlayerAdded:Connect(PlayerAdded)
1 Like

This is correct. I believe he only wants to do this once so he could just put this at the top of his script:

if Players:GetPlayers() == 0 then
    Players.PlayerAdded:Wait()
end
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.