Jump to content

getRandomPlayer


-misterX-

Recommended Posts

Posted

You can create a function which will loop all players, ignoring the local player, then insert on a table and return a random one.

getElementsByType 
math.random 

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

With these two functions and Lua knowledge.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted
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)] 

Posted

@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.

Posted
@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.

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...