Jump to content

Vehicle spawner


Recommended Posts

Posted

I have created a vehicle spawn script but when 2 players walk into the marker the same time.

It causes the vehicle's to spawn in each other en eventually explode.

Is there some way i could prevent this situation from happening?

Posted

I suppose your code is server side, you have to store the vehicle in a table for each player.

Showing your code would be better.

Please do not PM me with scripting related question nor support, use the forums instead.

Posted

Yes it is server side, I want to prevent a ambulance (416) to spawn inside the marker if a other player or vehicle is inside the marker.
How could I prevent this?

Spoiler

medicVehicleSpanwer = createMarker (1179.71, -1338.31, 13.00, "cylinder", 2, 255, 255, 255, 120)

local vehicleTable = {}

function giveAmbulance(hitElement, matchingDimension)
	if (getElementType (hitElement) == "player" and getTeamName(getPlayerTeam(hitElement)) == "Paramedic") then
		local theVehicle = getPedOccupiedVehicle (hitElement)
		if not theVehicle then
			local jobVehicle = vehicleTable[hitElement]
			if jobVehicle then
				if isElement(jobVehicle) then
					destroyElement(jobVehicle)
				end
				vehicleTable[hitElement] = nil
			end
			jobVehicle = createVehicle (416, 1179.71, -1338.31, 14.00, 0, 0, 270)
			setElementData (jobVehicle, "fuel", 100)
			warpPedIntoVehicle (hitElement, jobVehicle)
			vehicleTable[hitElement] = jobVehicle
		end
	end
end
addEventHandler ("onMarkerHit", medicVehicleSpanwer, giveAmbulance)

addEventHandler("onPlayerQuit", root,
function ()
    local jobVehicle = vehicleTable[source]
    if jobVehicle then
        if isElement(jobVehicle) then
            destroyElement(jobVehicle)
        end
        vehicleTable[source]= nil
    end
end)

 

 

  • Moderators
Posted (edited)
13 minutes ago, Potato_Tomato420 said:

How could I prevent this?

 

1. You can get the colshape of a marker.

https://wiki.multitheftauto.com/wiki/GetElementColShape

 

2. Get the elements of a specific type and check if there are none.

https://wiki.multitheftauto.com/wiki/GetElementsWithinColShape

 

 

local colshape = getElementColShape ( marker )

local players = getElementsWithinColShape ( colshape, "player" )
local vehicles = getElementsWithinColShape ( colshape, "vehicle" )
if #vehicles == 0 and #players == 0 then
  
end

 

Note: you might also need to check if the elements are not in the same dimension + interior, if you make use of those.

Edited by IIYAMA

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

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