God Mode

Is there god mode for players?
this is not working

RegisterCommand('god', function(source, args)
    god = not god
    if god then
    SetEntityInvincible(GetPlayerPed(-1), true)
    QBCore.Functions.Notify("~g~God Mode On")
  else
    SetEntityInvincible(GetPlayerPed(-1), false)
    QBCore.Functions.Notify("~r~God Mode Off")
    end
  end)

Is your variable god even defined somewhere above?

And yes there is a native for setting the players invincibility: SetPlayerInvincible - FiveM Natives @ Cfx.re Docs.

Also, for referencing the local ped you should use PlayerPedId() instead of GetPlayerPed(-1).

It would also be great if you could indent your code correctly when posting it on the forums and asking for help :smiley:

So your code would somewhat look like this (I’m usually not spoon feeding anyone…):

local hasGodModeEnabled = false

RegisterCommand("toggleGodMode", function(source, args, rawCommand)
    hasGodModeEnabled = not hasGodModeEnabled

    -- security checks? -> move this command to the server site

    local playerPed = PlayerPedId()
    if hasGodModeEnabled then
        SetPlayerInvincible(playerPed, true)
    else
        SetPlayerInvincible(playerPed, false)
    end

    -- notification
    print("God mode is now " .. (hasGodModeEnabled and "enabled" or "disabled"))
end, false)

Thanks
This code does not belong to me, I found it on the forum.
and yet I’m dying