Learning Lua, need some help to debug

Hey everyone! I’m attempting to learn .lua for my own little hobby. I’ve went ahead with a go with some a simple script that essentially toggles vehicle extras by using chat commands (even though someone has probably done this).

Is there anything anyone would suggest to either make this better, or just make it less complicated? It is only supposed to toggle Extra 1.

Server.lua

RegisterCommand('extra1', function()
	TriggerClientEvent("extra1", -1)
end)

Client.lua

RegisterNetEvent('extra1')
AddEventHandler('extra1', function()
local extraId = 1
local ped = PlayerPedId()
if IsPedInAnyVehicle(ped, false) then
    local vehicle = GetVehiclePedIsUsing(ped)
    if vehicle ~= nil then
      if (DoesExtraExist(vehicle, extraId)) then
        if (IsVehicleExtraTurnedOn(vehicle, extraId)) then
                SetVehicleExtra(vehicle, extraId, 1)
            else
                SetVehicleExtra(vehicle, extraId, 0)
            end
        end
    end
end
end, false)

I appreciate all criticism, thanks everyone!

  1. There’s no need to put a RegisterCommand in a server script. Save yourself net events by just using RegisterCommand in your client file.

  2. This way you don’t need to trigger an event at all. Just put everything from 'extra1' event into the RegisterCommand

  3. You can probably get rid of IsPedInAnyVehicle as GetVehiclePedIsUsing would return nil (or 0, not 100%) if you aren’t in a vehicle anyway. So this extra check isn’t necessary.

1 Like

Thanks so much, however when I remove the registerCommand from my server.lua, it doesn’t appear when I type /ext in the chat. Is there a way to do this in my client.lua?

https://docs.fivem.net/docs/resources/chat/events/chat-addSuggestion/
This shows how to add chat suggestions, and the different parameters you can use.

1 Like

Thanks! I appreciate the help.