ESX SimpleGarages - Advanced lightweight garage system

It worked! Thank you so much cannot thank you enough. I had tried AB_simple garages before you sent me the fix and it was pushing .30ms for the resource and this one is at a nice .02.03 muchbetter on the system.

For others in this situation this is what my DB looks like:

The only thing missing from this script is: on server disconnect: put vehicle in last garage not the impound, seems oddly punishing for having a disconnect from the server.

And a "check to see if there is a vehicle already spawned in the location so people don’t drop vehicles on top of one another when pulling them out of the garage after a reset.

Lastly if you police impound a vehicle from the police menu it goes to normal impound.
If you use /policeimpound you have to be inside the vehicle and then it goes to police impound.

I’ve tried: in config

Config.StoreOnServerStart = true -- Store all vehicles in garage on server start?

and in main.lua

if Config.StoreOnServerStart then
	AddEventHandler('onMySQLReady', function()
		local impounded = true
		if impounded then 
			TriggerEvent('AS_SimpleGarage:checkImpounded')
			--TriggerServerEvent('AS_SimpleGarage:checkImpounded') IF THIS BREAKS IT REVERT TO THIS.
		elseif not impounded then
		MySQL.Async.execute("UPDATE owned_vehicles SET `stored`=1 WHERE `stored`=0", {})
		end
	end)
end

Glad to hear that.

I’m gonna rewrite the spawn function for you. Please always make a backup. I am not testing this code:


spawnVehicle = function(vehicle, fuel, enginehealth, plate)
    --- test code for TristalNYC 
	--- this function will compare all the plates in the city, if the plate is found in the city it won't spawn the vehicle
	local gameVehicles = ESX.Game.GetVehicles() 
		for i = 1, #gameVehicles do
			local vehicle = gameVehicles[i]
			if DoesEntityExist(vehicle) then
				if Config.Trim(GetVehicleNumberPlateText(vehicle)) == Config.Trim(data.current.value.plate) then
					exports['mythic_notify']:SendAlert('error', ('This vehicle is already in the city!'))
					return
				end
			end
		end
	
	if not ESX.Game.IsSpawnPointClear(currentGarage.spawnPoint.coords, 3.0) then 
		exports['mythic_notify']:SendAlert('error', 'Please wait until the spawn point is free.')
		return
	end

	ESX.Game.SpawnVehicle(vehicle.model, currentGarage.spawnPoint.coords, currentGarage.spawnPoint.heading, function(veh)
		ESX.Game.SetVehicleProperties(veh, vehicle)
        SetEntityAsMissionEntity(veh, true, true)
        TaskWarpPedIntoVehicle(GetPlayerPed(-1), veh, -1)

        SetVehicleEngineHealth(veh, enginehealth + 0.0)
        exports['LegacyFuel']:SetFuel(veh, fuel)
        SetVehicleEngineOn(veh, true, true)
    end)
    -- Update the store state from the vehicle: 0 = impound(or in the city), 1 = garage, 2 = police impound
	-- If you change the car stored state to 1 they won't go to the impound but stay at the garage the problem is that they will
	-- be able to infinite spawn the same vehicle
	-- you can fix that by adding a check to see if the car is already in the city <= added this at the start already
    TriggerServerEvent('esx_simplegarages:server:updateCarStoredState', plate, 1)
end

Please let us know if this works for you.

First off you are a god. Second… little issue:

Getting nil for global data on this line 247main.lua (24.2 KB)

	if Config.Trim(GetVehicleNumberPlateText(vehicle)) == Config.Trim(data.current.value.plate) then

Please add this at the end of your Config. I do believe you don’t need to trim and i dont remember anymore why i added the trim. It’s been some time since i touched this script.

Config.Trim = function(value)
    if value then
        return (string.gsub(value, "^%s*(.-)%s*$", "%1"))
    else
        return nil
    end
end

Already had that at the end of my config. Was the first thing i fixed with the missing “end”

Add 2 prints to your console log to see which value is coming back null.

 if DoesEntityExist(vehicle) then
			print(GetVehicleNumberPlateText(vehicle))
			print(vehicleProps["plate"])
			if Config.Trim(GetVehicleNumberPlateText(vehicle)) == Config.Trim(vehicleProps["plate"]) then
				exports['mythic_notify']:SendAlert('inform', 'Vehicle is already out of the garage', 5000, { ['background-color'] = Config.ColorHex, ['color'] = '#ffffff' })
				return HandleCamera(cachedData["currentGarage"])
		   
			end
		end

Im not sure… try with vehicleProps.plate instead of vehicleProps[“plate”] .

Same error also just so ya know this prevents you from spawning the vehicle at all from the garage, before and after that change.

main.lua (15.7 KB)

Yeah, as soon as you want to spawn a vehicle, it gets the error and jumps out of the function. I will need you to send me your whole script to be able to test. I need to see whats inside vehicleProps to be able to get the plate out of there.

1 Like

The script is attached above! Thank you btw you are really helpful! Entire thing attached here:
esx_simplegarages.rar (18.7 KB)

Can you send it to me? I love that one. It would be a great help for me thanks!

spawnVehicle = function(vehicle, fuel, enginehealth, plate)
    --- test code for TristalNYC 
	--- this function will compare all the plates in the city, if the plate is found in the city it won't spawn the vehicle
	local gameVehicles = ESX.Game.GetVehicles() 
		for i = 1, #gameVehicles do
			local vehicletest = gameVehicles[i]
            if DoesEntityExist(vehicletest) then
                if Config.Trim(GetVehicleNumberPlateText(vehicletest)) == Config.Trim(vehicle.plate) then
                    exports['mythic_notify']:SendAlert('inform', 'Vehicle is already out of the garage', 5000, { ['background-color'] = Config.ColorHex, ['color'] = '#ffffff' })
                    return HandleCamera(cachedData["currentGarage"])
               
                end
            end
		end
	
	if not ESX.Game.IsSpawnPointClear(currentGarage.spawnPoint.coords, 3.0) then 
		exports['mythic_notify']:SendAlert('error', 'Please wait until the spawn point is free.')
		return
	end

	ESX.Game.SpawnVehicle(vehicle.model, currentGarage.spawnPoint.coords, currentGarage.spawnPoint.heading, function(veh)
		ESX.Game.SetVehicleProperties(veh, vehicle)
        SetEntityAsMissionEntity(veh, true, true)
        TaskWarpPedIntoVehicle(GetPlayerPed(-1), veh, -1)

        SetVehicleEngineHealth(veh, enginehealth + 0.0)
        exports['LegacyFuel']:SetFuel(veh, fuel)
        SetVehicleEngineOn(veh, true, true)
    end)
    -- Update the store state from the vehicle: 0 = impound(or in the city), 1 = garage, 2 = police impound
	-- If you change the car stored state to 1 they won't go to the impound but stay at the garage the problem is that they will
	-- be able to infinite spawn the same vehicle
	-- you can fix that by adding a check to see if the car is already in the city <= added this at the start already
    TriggerServerEvent('esx_simplegarages:server:updateCarStoredState', plate, 1)
end

There was a couple of errors, should be working now. Sorry for the late answer but just got home.

Can you please be more specific? What do you need?

Thanks for code fix!

bro could you dm the modified script the original is not working for me

Thank you going to give this a try! Will get back to you soon, away from the comp for a few days.

im getting an error saying esx_simplegarages/client/main.lua:24: attemp to index a nil value (global ‘ESX’)

I need the private garage script that you added and also with the check so players dont spawn vehicles on top of each other. The animations also mate. Thank you I’ve been looking for this so long