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

My for _, player w'ont try to find a randomplayer ?

Asked by 5 years ago
while wait() do
    for i = 5,0,-1 do
        --Lobby Timer
        wait(sec)
        print(i.." / Before")
    end
    for _, player in pairs(game.Players:GetPlayers()) do
        --Player Monster
        local randomplayer = player[math.random(1, #player)]
    end
end

The output say that player is a userdata value and i d'ont know how to fix this

3 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

well, the reason might be that player, is well... a player object and not a table so you might want to try something on the lines of this

math.randomseed(tick())
while true do
    local plrs = game.Players:GetPlayers()
    local rIndex = math.random(1,#plrs)
    local randomplr = plrs[rIndex]
    wait()
end

So things to take away:

1) don't use while wait() do end, use while true do wait() end
2) use math.randomseed(tick()) for "better randomness"
0
What's erong with while wait() do?? SirDerpyHerp 262 — 5y
0
*wrong SirDerpyHerp 262 — 5y
Ad
Log in to vote
0
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
5 years ago

If you're trying to select a random player, that's not the way to do it.

Try this:

while wait() do
    for i = 5,0,-1 do
        --Lobby Timer
        wait(sec)
        print(i.." / Before")
    end
    local players = game.Players:GetPlayers()
    local randomplayer = players[math.random(1, #players)]
end
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

a for loop literates through a table, and the variable is the object, not a table which you'd use a # operator to get a length. this means you dont need to use a for loop to get a random player, but just use game.Players:GetPlayers(), math.random and the # operator alone.

 while true do
    for i = 5,0,-1 do
        --Lobby Timer
        wait(sec)
        print(i.." / Before")
    end
    local players = game.Players:GetPlayers()
    local chosenPlayer = players[math.random(1,#players)]

    print(chosenPlayer.Name) -- example
    -- do stuff here

    -- you dont need a wait as theres a for loop which yields
end

Answer this question