Jump to content

random spawn points


jkub

Recommended Posts

I have made 4 spawn points and I would like to where when someone joins they spawn randomly to one of those points using the mat.random function. ive tried this but it seems to be inaffective

g_root = getRootElement() 
spawnPoints = { } 
  
function spawnPlayer ( ) 
    spawnPoints[1] = 0, 0, 5 
    spawnPoints[2] = 0, 500, 5 
    spawnPoints[3] = 500, 0, 5 
    spawnPoints[4] = 1000, 0, 5 
    randomSpawn = math.random ( 1, #spawnPoints ) 
        spawnPlayer ( source, randomSpawn ) 
end 
  
addEventHandler ( "onPlayerJoin", g_root, spawnPlayer ) 

Link to comment
I have made 4 spawn points and I would like to where when someone joins they spawn randomly to one of those points using the mat.random function. ive tried this but it seems to be inaffective

Ok it's close, but the randomSpawn is only an integer between 1 and #spawnPoints (number in the array) you need to put this:

g_root = getRootElement() 
spawnPoints = { } 
  
function spawnPlayer ( ) 
    spawnPoints[1] = 0, 0, 5 
    spawnPoints[2] = 0, 500, 5 
    spawnPoints[3] = 500, 0, 5 
    spawnPoints[4] = 1000, 0, 5 
    randomSpawn = math.random ( 1, #spawnPoints ) 
        spawnPlayer ( source, spawnPoints[randomSpawn] ) 
end 
  
addEventHandler ( "onPlayerJoin", g_root, spawnPlayer ) 

That should be ok :)

Link to comment

Kinda odd nobody noticed declaring a var like

var = value, anothervalue, anothervalue...

you can assign only one value to a variable.. So I would just make them into a table

  
spawnPoints[1] ={ 0, 0, 5} 
spawnPoints[2] = {0, 500, 5} 
spawnPoints[3] = {500, 0, 5} 
 spawnPoints[4] = {1000, 0, 5} 
  

and later use

spawnPlayer(source, spawnPoints[randomSpawn][1],spawnPoints[randomSpawn][2],spawnPoints[randomSpawn][3]) 

Link to comment
Kinda odd nobody noticed declaring a var like

var = value, anothervalue, anothervalue...

you can assign only one value to a variable.. So I would just make them into a table

  
spawnPoints[1] ={ 0, 0, 5} 
spawnPoints[2] = {0, 500, 5} 
spawnPoints[3] = {500, 0, 5} 
 spawnPoints[4] = {1000, 0, 5} 
  

and later use

spawnPlayer(source, spawnPoints[randomSpawn][1],spawnPoints[randomSpawn][2],spawnPoints[randomSpawn][3]) 

or this should work too:

spawnPlayer( source, unpack( spawnPoints[ randomSpawn ] ) ); 

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