Jump to content

onVehicleSpawn Event


ShayF2

Recommended Posts

I didn't want to put this into resources because it's not a resource, its more of a useful function. I made this today and I thought that it would help a lot of scripters.
This is a onVehicleSpawn event that I made, it is server side only.
Here is how you use it.

addEvent('onVehicleSpawn')
local last = 0
setTimer(function()
	local vehs = getElementsByType('vehicle')
	if #vehs > last then
		last = #vehs
		triggerEvent('onVehicleSpawn',root,vehs[#vehs])
	end
end,50,0)
-- then use it like so

addEventHandler('onVehicleSpawn',root,function(vehicle)
	setElementPosition(vehicle,0,0,0)
	-- or whatever u want here..
end)

 

Edited by ShayF
  • Thanks 1
Link to comment

very useful. It was so hard to trigger client many times to get a vehicle with isElementStreamedIn. But still this function may have problems. For example when 2 cars were spawned at the same time. Also it might reduce server perfomance when it has more than 200 players

Edited by Juuve
Link to comment
  • Moderators

Hmm interesting.

 

It isn't bug free yet.

For example if you destroy a vehicle and create a vehicle at the same time, the event wouldn't be triggered.

A more accurate way would be using an invert loop, which checks if the vehicle's at the end of the table are new. If not, then you can break the loop. New vehicle's should be afaik always at the end.

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