Just started studying Lua and FiveM programming need some help with error handling


Client script:

local eventData = AddEventHandler('spawncar', function(model)
	local hash = GetHashKey(model)

		while not HasModelLoaded(hash) do

	local x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(-1), false))
	local vehicle = CreateVehicle(hash, x + 2, y + 2, z + 1, 0.0, true, false)

Server script:

RegisterCommand('createcar', function(source,args,rawCommand)
	if args[1] == nil then
		print("Error you have not typed in the name of the vehicle you're trying to spawn!")
		TriggerClientEvent('spawncar', source , *args[1])

end, false)

My goal: CreateVehicle returns 0 if there is no such model to be spawned. I want to handle that as an error and print out an message. Only issue is that print only seems to work on server script. My idea was making an pointer at first to modify args[1] then handle the error from there, but from what I googled there’s no such option for this. How do people handle error printing if code is executed on client side? ( I didn’t manage to find a post about this so I apologize if there’s an thread about this already, cheers! )

Edit: By error handling I mean this:

if vehicle == nil then
     print("Vehicle model you're trying to spawn doesn't exist")
     print("You have spawned " ... vehicle ... "!")

I think this explains it enough.


btw, you do not need to create a thread every time you want to make a while loop.


Seems like good practice running a thread parallel to the main thread to execute loops without disrupting the main thread? Using more resources to have your code execute quicker? ( From what I understand )


print works for me on client, but it prints to the client console F8


As @Frazzle said, print on client prints to the F8 console.


You can also RegisterCommand on the client, which might print output to the chat window. Alternately, trigger a chat:addMessage event or use notification natives.


Thank you this is what I was looking for. @californium


For future refrences if anyone stumbles upon this thread the solution was:

	if vehicle == 0 then
		TriggerEvent('chatMessage', "Vehicle you're trying to spawn doesn't exist")
		local vehicleName = GetDisplayNameFromVehicleModel(hash)
		vehicleName = string.lower(vehicleName)
		TriggerEvent('chatMessage', "You have spawned an " .. vehicleName .. "!")