Jump to content

getRandomPlayer


-misterX-

Recommended Posts

Server-side:
local rndPlayer; -- variable holding random player 
while( player == rndPlayer ) do 
    rndPlayer = getRandomPlayer(); 
end 

It just hurts to see that while loop...

local players = {} 
for k, player in ipairs(getElementsByType("player"))do 
    if(player ~= localPlayer)then 
        table.insert(players, player) 
    end 
end 
  
local randomPlayer = players[math.random(#players)] 

Link to comment

@arezu,

Yours is worse, why? Because you have to loop through all the players and create a table of the players. Imagine 30 players online, you have to loop through them all. The chances of my loop to repeat 30 times is very low since getRandomPlayer would have to get the same player 30 times which is unlikely to happen. If the script needs to be client-side script then there is no other way than using the loop that you showed.

Link to comment
@arezu,

Yours is worse, why? Because you have to loop through all the players and create a table of the players. Imagine 30 players online, you have to loop through them all. The chances of my loop to repeat 30 times is very low since getRandomPlayer would have to get the same player 30 times which is unlikely to happen. If the script needs to be client-side script then there is no other way than using the loop that you showed.

I forgot that getRandomPlayer doesn't loop all players.

But anyways:

how do i gat a random player exept the player i'm using? i mean get someone connected to the server exept the local player

I guess this means he wants it client sided, so getRandomPlayer wont work.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...