Jump to content

[Solved]Random Spawn location


Xierra

Recommended Posts

Hello guys.

I wanted to make more than one spawn areas at once.

I have edited the broph.lua like this:

addEventHandler("onResourceStart", resourceRoot,
function()
for i,player in ipairs(getElementsByType("player")) do
		spawn(player)
end
end
)
 
function spawn(player)
 
local spawns = { }
spawns[1].x = -2030.1610107422
spawns[1].y = 172.25221252441
spawns[1].z = 28.8359375
spawns[1].rot = 90.0
 
spawns[2].x = 423.2495
spawns[2].y = 453.2564
spawns[2].z = 564.6321
spawns[2].rot = 120.6928
 
local rnd = math.random( #spawns )
spawnPlayer( player, spawns[rnd].x, spawns[rnd].y, spawns[rnd].z, spawns[rnd].rot )
end
 
addEventHandler("onPlayerJoin", root,
function()
	spawn(source)
end
)
 
addEventHandler("onPlayerWasted", root,
function()
setTimer(spawn, 3000, 1, source)
end
)

But at the game it says (with debugscript 3)

ERROR: MTA SA deathmatch/server/mods/resources/play/broph.lua:12: attempt to index field '?' (a nil value)

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

A little edit:

To anyone who wanted the fixed version, just copy the lines!

addEventHandler("onResourceStart", resourceRoot,.
function()
for i,player in ipairs(getElementsByType("player")) do
              spawn(player)
end
end
)
 
function spawn(player)
local spawns = { }
                spawns[1] = { }
                spawns[1].x = -2030.1610107422
                spawns[1].y = 172.25221252441
                spawns[1].z = 28.8359375
                spawns[1].rot = 90.0
 
                 spawns[2] = { }
                spawns[2].x = 423.2495
                spawns[2].y = 453.2564
                spawns[2].z = 564.6321
                spawns[2].rot = 120.6928
 
local rnd = math.random( #spawns )
spawnPlayer( player, spawns[rnd].x, spawns[rnd].y, spawns[rnd].z, spawns[rnd].rot )
end
 
addEventHandler("onPlayerJoin", root,
function()
           spawn(source)
end
)
 
addEventHandler("onPlayerWasted", root,
function()
setTimer(spawn, 3000, 1, source)
end
)

Edited by Guest
Link to comment

You can create a multi-array table, which will contain x,y,z and rz values like this:

local spawns = {
--x, y, z, rotZ
{ -2030.1610107422, 172.25221252441, 28.8359375, 90.0 },
{ 423.2495, 453.2564, 564.6321, 120.6928 }
}
 
function spawn( player )
local rnd = math.random( 1, #spawns )
spawnPlayer( player, spawns[rnd][1], spawns[rnd][2], spawns[rnd][3], spawns[rnd][4] )
end

:wink:

Link to comment

error was in spawns[1].x, cause spawns is a table, while spawns[1] is not.

you need to make a table before indexing it:

local spawns = { }
  spawns[1] = { }
  spawns[1].x = -2030.1610107422
  spawns[1].y = 172.25221252441
  spawns[1].z = 28.8359375
  spawns[1].rot = 90.0
 
  spawns[2] = { }
  spawns[2].x = 423.2495
  spawns[2].y = 453.2564
  spawns[2].z = 564.6321
  spawns[2].rot = 120.6928

etc. basically, the same thing DaK did *)

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