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 6 years ago
01while wait() do
02    for i = 5,0,-1 do
03        --Lobby Timer
04        wait(sec)
05        print(i.." / Before")
06    end
07    for _, player in pairs(game.Players:GetPlayers()) do
08        --Player Monster
09        local randomplayer = player[math.random(1, #player)]
10    end
11end

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 6 years ago
Edited 6 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

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

So things to take away:

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

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

Try this:

1while wait() do
2    for i = 5,0,-1 do
3        --Lobby Timer
4        wait(sec)
5        print(i.." / Before")
6    end
7    local players = game.Players:GetPlayers()
8    local randomplayer = players[math.random(1, #players)]
9end
Log in to vote
0
Answered by 6 years ago
Edited 6 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.

01while true do
02    for i = 5,0,-1 do
03        --Lobby Timer
04        wait(sec)
05        print(i.." / Before")
06    end
07    local players = game.Players:GetPlayers()
08    local chosenPlayer = players[math.random(1,#players)]
09 
10    print(chosenPlayer.Name) -- example
11    -- do stuff here
12 
13    -- you dont need a wait as theres a for loop which yields
14end

Answer this question