Need help figuring out an issue

Hello FiveM community,
I’m doing some experiments scripting with LUA, before doing my first script.
So I’m trying to create a livery changer script, that should work as follows:
When a player presses L (182), it changes the livery of the car to the next one.

I commented the code to make it easier to understand (although it’s really simple and clear).

__resource.lua

client_script 'client.lua'

client.lua

function changeLivery(vehicle) -- Function to change the livery
		-- It changes the livery between the first one (0) and the 10th one (9)
		if livery < 9 then -- If the current livery is between 0 and 8 (we are still able to go further)
			livery = livery + 1 -- So we change to the next one
		else if livery == 9 then -- If the current livery is the last one (9)
			livery = 0 -- Since we can't change to the nexxt one (cause the next is 10, which does not exist), we change it back to 0 (first one)
		end

		SetVehicleLivery(vehicle, livery) -- Native function to set the livery of the vehicle to the above specified livery

	    DisplayHelpText("~b~Livery ~g~Changed.") -- Success message

end

Citizen.CreateThread(function()
	while true do -- Loop that triggers the livery changer function
		Wait(0)
		ped = GetPlayerPed(-1)

		if IsPedInAnyVehicle(ped, false) then -- Only be able to change the livery if the player is in a vehicle
			vehicle = GetVehiclePedIsUsing(ped) -- Get the player vehicle's entity
			livery = GetVehicleLivery(vehicle) -- Current livery

			if IsControlJustPressed(1, 182) then -- If player presses the L key
				changeLivery(vehicle) -- The livery changer function gets triggered, on the player's vehicle
		else 
			DisplayHelpText("~b~You must be in a vehicle in order to change your livery!") -- Error message
		end
	end
end)

Thanks so much in advance for your help, cause I would be really happy as well as infinitely thankful if someone could help me out! :grin:

So what part of the code is not working?

1 Like

How may I know that? Cause it does not give me any error

By debugging the code…
See if it actually calls the livery native (i.e. put a print above it)

1 Like

Alright, I will try to do that. I’ll tell you the result. Thx ^^

I added these prints to debug, in every function and event to see if they get triggered, but neither a single print showed up in the F8 console…

function changeLivery(vehicle) -- Function to change the livery
		print('changeLivery function loaded')
		-- It changes the livery between the first one (0), to the 10th one (9)
		if livery < 9 then -- If the current livery is between 0 and 8 (we are still able to go further)
			livery = livery + 1 -- So we change to the next one
		else if livery == 9 then -- If the current livery is the last one (9)
			livery = 0 -- Since we can't change to the nexxt one (cause the next is 10, which does not exist), we change it back to 0 (first one)
		end

		SetVehicleLivery(vehicle, livery) -- Native function to set the livery of the vehicle to the above specified livery
		print('Livery should''ve been changed now')
	    DisplayHelpText("~b~Livery ~g~Changed.") -- Success message

end

Citizen.CreateThread(function()
	while true do -- Loop that triggers the livery changer function
		print('The script is OK')
		Wait(0)
		ped = GetPlayerPed(-1)

		if IsPedInAnyVehicle(ped, false) then -- Only be able to change the livery if the player is in a vehicle
			print('Vehicle enter event TRIGGERED')
			vehicle = GetVehiclePedIsUsing(ped) -- Get the player vehicle's entity
			livery = GetVehicleLivery(vehicle) -- Current livery

			if IsControlJustPressed(1, 182) then -- If player presses the L key
				print('L key press event TRIGGERED')
				changeLivery(vehicle) -- The livery changer function gets triggered, on the player's vehicle
		else 
			DisplayHelpText("~b~You must be in a vehicle in order to change your livery!") -- Error message
		end
	end
end)

Which operating system are you using?

1 Like

I usually use Linux for my server but this test server I’m running it is Windows. So yes, both client and server are Windows 10.

Hmm ok, that should be just fine. So I only see 2 options why this is not working:

A) Resource doesn’t get loaded or started
B) Citizen.CreateThread is broken.

To test both theories, put a print at the top of the script (outside the thread and function). Let me know if the print shows up.

1 Like

Yeah I added the print to the absolute top of the script (out of both the function and the thread), and the print did not show up at all, although the resource seems to be loading when it downloads all the resources on joining the server.

Can you put you __resource.lua and server.cfg into a pastebin?

And what is the name of the resource (the folder name in the resources)

1 Like

Here it is:

server.cfg: https://pastebin.com/r7CKCKPm
Name of the resource folder: liverychanger
__resource.lua: https://pastebin.com/dyBEtHju
client.lua: https://pastebin.com/vpARkTbS

Now I see I missed an end key in line 31, although I don’t think that fault is causing all this.

  1. Does the console state that liverychanger is started?
  2. Does the console give any errors?
  3. Is __resource.lua at resource/liverychanger/__resource.lua?

Nothing seems out of the ordinary regarding the server cfg, resource manifest and paths/code.
Could you share your server log? (in the artifacts)

1 Like
  1. Yes, it states it: image
  2. It doesn’t give a single error.
  3. __resource.lua is located at resources/liverychanger/__resource.lua

Ok, I’m gonna upload the server log

Im at a lost here :confused:

1 Like

:sleepy:

Log:
https://pastebin.com/D4TjpcvF

Yeah I see nothing wrong here, something you could try:

  1. Remove other resources (and add them one by one back if they work).
  2. Update server artifacts.
1 Like

Thanks so much anyway for trying to help me man. We need more people like you in this community :blush:

I created a brand new stock server with latest build (366) and remade the resource with new files just in case, and it’s still not working… I hope I can fix this someday :persevere: