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

Client script:

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


	Citizen.CreateThread(function()
		RequestModel(hash)
		while not HasModelLoaded(hash) do
			Citizen.Wait(0)
		end
	end,false)

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

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!")
	else
		TriggerClientEvent('spawncar', source , *args[1])
	end


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")
else
     print("You have spawned " ... vehicle ... "!")
end

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

1 Like

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.

2 Likes

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")
	else
		local vehicleName = GetDisplayNameFromVehicleModel(hash)
		vehicleName = string.lower(vehicleName)
		TriggerEvent('chatMessage', "You have spawned an " .. vehicleName .. "!")
	end