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

What am I doing wrong?

Asked by 9 years ago

This script is supposed to get 1 random player out of the players service and teleport him when he/ she is chosen. ERROR :bad argument #2 to 'random' (interval is empty)

players = game.Players:GetChildren()

while true do wait() playerchose = math.random(1,#players) playerchosen = players[playerchose] playerchosen.Character.Torso.CFrame = CFrame.new(Vector3.new(10,10,10)) wait(2) end end

0
If an answer helped you, make sure to accept it. TheLuaWeaver 301 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

The issue is that the first argument is higher than the second argument. You're probably wondering how that's possible; the issue is that the script is loaded as soon as the game starts, when there are no players. That means #players is 0, so you're doing math.random(1,0), which means "get a random integer higher than 1 and less than 0", and there are no numbers like that!

What you need to be doing is something more like

while true do
    wait(2)
    local players=game.Players:GetPlayers()
    if #players>0 then
        local randomPlayer=players[math.random(#players)]
        --do stuff with randomPlayer
    end
end
0
Thanks so much! @TheluaWeaver PullAnAllNighter 8 — 9y
Ad

Answer this question