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!

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)

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.