Jump to content

CreateExplosion


DiosSasuke

Recommended Posts

Hi, can someone tell me what I did wrong with this script? I'm new to this? In the console I see server.lua: 2: Bad argument @ `getElementPosition` [Expected element at argument 1, got nil]

and I also see server.lua: 3: Bad argument @ `createexplosion` [Expected vector3 at argument 1, got boolean]

here I leave the lua
 

function explosionOnSpawn ( )
  local pX, pY, pZ = getElementPosition(source)
  createExplosion ( pX, pY, pZ, 6, source )
end
addEventHandler ( "onPlayerSpawn", getRootElement(), explosionOnSpawn )
addCommandHandler ( "explosion", explosionOnSpawn )

 

Link to comment

How is the explosionOnSpawn called? Is it through the event or command handler? Because if you're using the command then source is nil, hence getElementPosition returns false.

To make it work in both cases you have to check if source ~= nil or if the first parameter ~= number (as onPlayerSpawn's first parameter is a number).

Something like this should work:

function explosionOnSpawn (player)
  if not source then --Source is nil, so we know that it was the command that executed it.
  	source = player --make source be the 'player'
  end
  local pX, pY, pZ = getElementPosition(source)
  createExplosion ( pX, pY, pZ, 6, source )
end
addEventHandler ( "onPlayerSpawn", getRootElement(), explosionOnSpawn )
addCommandHandler ( "explosion", explosionOnSpawn )

 

Link to comment
8 hours ago, DiosSasuke said:

Thanks, but how do I do it so that it does not take away my life?
and also how do I create an object? but not that the object disappears

 

 

Hello, if you do not want your life to go down I recommend you do a "triggerClientEvent". ( this function is shared but we must pass it because the arguments we need are on the client side ).

This is an example:

 

server:

--your code
function explosionOnSpawn (player)
  if not source then --Source is nil, so we know that it was the command that executed it.
  	source = player --make source be the 'player'
  end

--The trigger
triggerClientEvent(source,"onThePlayerSpawn",source)  
  
end
addEventHandler ( "onPlayerSpawn", getRootElement(), explosionOnSpawn )
addCommandHandler ( "explosion", explosionOnSpawn )

client:

addEvent("onThePlayerSpawn", true)
addEventHandler("onThePlayerSpawn", root, 
function()
    
local x,y,z = getElementPosition (localPlayer) 
createExplosion ( x, y, z, 6, false, -1.0, false ) 
    
end   
)  

 

Edited by <~KaMiKaZe~>
  • Thanks 1
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...