Jump to content

random spawn points


jkub

Recommended Posts

Posted

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 ) 

crysis-02.png
Posted
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 :)

Posted

It's kind of odd how nobody noticed the potential infinite loop. Rename your function.

Do NOT PM ME for help unless invited. - New MTA Script Editor

Scripting help "etiquette": understandable language, relevant code (ALL code if unsure), [Lua] tags, error messages with line numbers. Super simple stuff.

Posted

Actually, yes, it's an infinite loop, since there's no way it'll break once it's called...

Do NOT PM ME for help unless invited. - New MTA Script Editor

Scripting help "etiquette": understandable language, relevant code (ALL code if unsure), [Lua] tags, error messages with line numbers. Super simple stuff.

Posted

When I put my first version of this in debug It did say somthing about a "stack overflow" Could that be what you guys are referring to?

crysis-02.png
Posted

Yes. Just rename your function and you should be fine.

Do NOT PM ME for help unless invited. - New MTA Script Editor

Scripting help "etiquette": understandable language, relevant code (ALL code if unsure), [Lua] tags, error messages with line numbers. Super simple stuff.

Posted

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

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

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