Livery command

I am trying to create a livery command, got it about 1/2 way working right now.

-- Livery --
AddEventHandler('chatMessage',function(player, playerName, message)
	local msg = string.lower(message)
	
	if msg == '/livery' then
		TriggerClientEvent('VehicleLivery', player)
		CancelEvent()
	end
end)
-- Livery --
RegisterNetEvent('VehicleLivery')
AddEventHandler('VehicleLivery', function()
	local Veh = GetVehiclePedIsIn(GetPlayerPed(-1))

	SetVehicleLivery(Veh, livery) --CHANGE livery(id)
end)

It is changing the livery but only to the first livery in the file. Anyone wanna help me set this up so I could cycle through or even do /livery 1 or /livery 2

1 Like

There are better solutions for this to count liveries and do cycling, but this will enable you to do your /livery 0 /livery 1 /livery etc thing. Are you using a custom chat and cannot register commands?

-- Livery Server--
AddEventHandler('chatMessage',function(player, playerName, message)
	local msg = split(message, ' ')
	
	if msg[1] == '/livery' then
		TriggerClientEvent('VehicleLivery', player, msg[2])
		CancelEvent()
	end
end)

function split(s, delimiter)
    result = {};
    for match in (s..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match);
    end
    return result;
end

-- Livery Client--
RegisterNetEvent('VehicleLivery')
AddEventHandler('VehicleLivery', function(livery)
	local Veh = GetVehiclePedIsIn(GetPlayerPed(-1))

	SetVehicleLivery(Veh, livery) --CHANGE livery(id)
end)
1 Like

We can use commands, was just going to put this code into our vehicle control script but I will try out what you sent, then I get to have the fun of doing extras for police vehicles lol

Ok Above is working perfect, thanks man! Appreciate it

Extras are a bit trickier but you can come up with some function that loops through a static range from x=0 to x<something (can’t recall how many slots was observed as a max) calling DoesExtraExist(vehicle, x) and then placing those results into an indexed table. From there the same approach of /extra 0, /extra 1, /extra 2 could be used to toggle on and off if you also use IsVehicleExtraTurnedOn(vehicle, x) to decide how to toggle.

1 Like

Yeah, figured it would be. Found someone he made a menu for it but would rather then menu show up when I type /extras or something, for now it is bound to f6 which wont work at the moment

You should use RegisterCommand :wink:

1 Like
RegisterCommand('livery', function(source, args, rawCommand)
	local Veh = GetVehiclePedIsIn(GetPlayerPed(-1))
  local livery = tonumber(args[1])

  SetVehicleLivery(Veh, livery) --CHANGE livery(id)
  drawNotification("Vehicle Livery ~r~"..livery.."~s~ loaded!")
end)

function drawNotification(Notification)
	SetNotificationTextEntry('STRING')
	AddTextComponentString(Notification)
	DrawNotification(false, false)
end

That should work, and its just a client file :stuck_out_tongue:

2 Likes

That works but for some reason I have to use F8 Console to do the command :confused: the other one works but it only changes the livery once.

1 Like

You are probably using a customized chat resource that does not recognize CFX registered commands. If the script I posted above works for you then try with other vehicles. A lot of them do not have any liveries, some only have 1 or 2. Try the “PONY” model, there is a livery for plain, one for sprunk, one for cleaners, and one for GoPostal. That should show you if it works more than once. /livery 0, /livery 1, /livery 2, /livery 3.

I have a car called the 2015polstang, It has multiple liveries, what I did was do /livery 2 but it went to the last livery, So I used what was posted above and it works completely fine just in the f8 menu sadly.

Hi where file i add this livery changes command on police car

hi, this very helpful and it is working! however I have a question:

what do I add in the script, so that people who have police job or ambulance job can use the livery command only?

currently, anyone can change livery - and I don’t want it to be the case

where do i add this code?