Using of server script instead

Hi here :slight_smile:
I have tried to spawn cars and PEDs with client scripts, no trouble it work well, but as everyone knows, probably not all spawns are experienced even for everyone when using client scripts.
I’m not a LUA expert and I would therefore like to ask for help. Can anyone here give me a few simple examples of how to spawne cars and / or PEDs with using server scripts instead, hope for help.

Use these:-

Server:
https://wiki.fivem.net/wiki/TriggerClientEvent

Client:
https://wiki.fivem.net/wiki/AddEventHandler
https://wiki.fivem.net/wiki/RegisterNetEvent

Hope that helps you :slight_smile:

Triggering events has nothing to do with spawning vehicles or peds… take a look at the native db and look for CreateVehicle() and to set the player’s ped skin use SetPlayerSkin()

Okayy and thanks.
but as I write, i’m not an lua expert. So have this a chance to work.

All is in the place, server.cfg, __resource.lua -----> server_scripts { place_car.lua }
And my lua file ‘place_car.lua’, with this:

AddEventHandler(‘place a car’, function ()
– Place Akuma
Vehicle CREATE_VEHICLE(0x63ABADE7, -40.529, -1100.762, 26.422, 118.0, BOOL isNetwork, BOOL thisScriptCheck)
end)

But what stands this fore: ‘BOOL isNetwork, BOOL thisScriptCheck’

Maybe I forgot this in my code:
RegisterServerEvent(‘place a car’)

:slight_smile:

I disagree, in this case the op already has the code client side, might as well put it to use :smiley:

what…
no code (examples) or descriptions in OP, so there’s no way to see what code is being used? Besides, what are you going to do with events for spawning vehicles/peds? absolutely nothing, you shouldn’t create local (non-networked) vehicles/peds and just “spawn them on all clients manually using events” if that’s what you’re trying to do.

Instead, spawn vehicles/peds as networked, and they will show up for everyone.

So either I’m losing my mind and completely failed to understand the OP’s question, or my reply about “using events in this case is useless” isn’t wrong?

Ahh, nope, I’m in the wrong here; it had not occurred to me Vehicles are networked, I was thinking that if the code was already in a client side script, but needed to be executed from a server script (for whatever reason), then events could be used. Also, never said you were wrong, just stated I disagreed :wink:

1 Like

True, I was just getting confused myself.

1 Like

This is my best ‘shot’, but it still don’t work. Can’t find ‘something’ with ‘meta’ ? :confused:

– *** This is only for test ***

RegisterServerEvent(‘placecar’)

AddEventHandler(‘placecar’, function ()

local ambulance = {"ambulance"}
local ambulanceHash = GetHashKey(ambulance[math.random(#ambulance)])

-- Place ambulance
CreateVehicle(ambulanceHash, -40.529, -1100.762, 26.422, 118.0, true, true)
SetVehicleFixed(ambulance)
SetVehicleOnGroundProperly(ambulance)

end)

I understand it’s a very complicated case to spawn cars and PEDs in a server script, since no one can come up with a kind of suggestion. So I have to go back to the client spawn, it works, but thank for participation :frowning: .

Try spawning vehicles with the last two parameters set to true, false), having it true true seems to break for me 9/10 times.

@Vespura… Thanks for answer, I will try that (coming back) :slight_smile:

No :frowning: … I have set to ‘false’ but it still not work. Here is my error message and hope for help:

[ 344] Found new resource my_test in C:/Users\Jan\Documents\Blandet\GTA\FiveM_Network\cfx-server\vRP_Server/resources//my_test
[ 375] Resource loading for my_test failed:
[ 375] Could not execute resource metadata file C:/Users\Jan\Documents\Blandet\GTA\FiveM_Network\cfx-server\vRP_Server/resources//my_test/__resource.lua: …cfx-server\vRP_Server/resources//my_test/__resource.lua:3: attempt to index a function value (global ‘place_car’)
[ 375] stack traceback:
[ 391] …cfx-server\vRP_Server/resources//my_test/__resource.lua:3: in local ‘chunk’
[ 391] citizen:/scripting/resource_init.lua:38: in function citizen:/scripting/resource_init.lua:1

  1. it looks like you’re installing your resource my_test in /resources/some_foldername/my_test/, try moving it to just /resources/my_test/
  2. Your __resource.lua file contains place_car somewhere, this doesn’t exist/it’s not something you can put in your __resource.lua file, what are you trying to do?
  1. I do not understand why the ‘console report’ make this two ‘//’. my_test is on the right place directly in the resource folder, so ??

  2. Here you can see, what my ‘__resource.lua’ contains. Maybe the problem is here ?:

server_scripts {
place_car.lua
}

I just try to spawn a car via server script and not as a client script of the reasons I have describe of previously causes :slight_smile: .

server_script 'place_car.lua'

or

server_scripts {
    'place_car.lua'
}

or even

server_script ('place_car.lua')
server_scripts( {
    'place_car.lua'
})

The ’ ’ are required.
https://wiki.fivem.net/wiki/Resource_manifest

what’s in that place_car.lua file?

My main problem was I have forgot ‘place_car.lua’, these two ( ’ ’ ), in the ‘__resource.lua’. Now I can load my function without error.
But it still don’t work, there are no ‘ambulance’ on the place I write, so next step please :sake:

*** My file: place_car.lua ***

RegisterServerEvent('placecar')

AddEventHandler('placecar', function ()

	local ambulance = {"ambulance"}
	local ambulanceHash = GetHashKey(ambulance[math.random(#ambulance)])
	
	-- Place ambulance
	CreateVehicle(ambulanceHash, -40.529, -1100.762, 26.422, 118.0, true, false)
	SetVehicleFixed(ambulance)
	SetVehicleOnGroundProperly(ambulance)

end)

You’re registering it as a server script, but the code is for a client script. So register it as a client script instead