SetEntityInvincible doesn't work

Hi, I’m doing a menu which includes a toggle item for invincibility
I’m sure that the menu is working because I’ve tried with other things, but the invincibility doesn’t work
Here’s the code that I’ve used:

SetEntityInvincible(GetPlayerPed(-1),false)

perhaps it does not work because you set it to false?

I’m sorry, I copied the wrong piece of code xD, in the right one it is set to true, this is the piece that disables the invincibility


local isInvincible = false

function toggleInvincibility()
    isInvincible = not isInvincible
    SetEntityInvincible(PlayerPedId(), isInvincible)
end

try this

I made a notify text to be sure that there are no problems in toggling, but with this code I’m still dying when I take damage. Am I invulnerable to everybody (like npcs), aren’t I? Or am I invulnerable only to other players?

   local isInvincible = false
   
   local Item3 = NativeUI.CreateCheckboxItem("Diventa invincibile", bool, "Attiva/disattiva l'invincibilità")
   submenu.OnCheckboxChange = function(sender, item, checked_)
        if item == Item3 then
            bool = checked_
		    
			Citizen.Wait(0.2)
			
			isInvincible = not isInvincible
            SetEntityInvincible(PlayerPedId(), isInvincible)
    		text = (tostring(isInvincible))
		    SetNotificationTextEntry("STRING")
			AddTextComponentString(text)
			DrawNotification(true, true)      
        end
    end

yeah just checked it does not work for me too
strange things happening.
It worked few days ago. May bee FiveM’s update caused this, not sure

Thank you, I’ll check in a few days then

Hi! Perhaps you could try using this native instead. (I haven’t had the time to check, but I hope it helps :slight_smile: )

Hi, thank you for the answer.
I’ve tried with this code:

      bool = checked_
		    
			Citizen.Wait(0.2)
			
			isInvincible = not isInvincible
           
			SetPlayerInvincible(PlayerId(), isInvincible)
			
    		text = ("bool value:" .. tostring(isInvincible))
		    SetNotificationTextEntry("STRING")
			AddTextComponentString(text)
			DrawNotification(true, true)    

            text2 = ("invincibility value:" .. tostring(GetPlayerInvincible(PlayerId())))
		    SetNotificationTextEntry("STRING")
			AddTextComponentString(text2)
			DrawNotification(true, true)	

When I activate the invincibility, the first notification (bool value) returns “true” and the second notification returns “1”, but I can still die (I’ve tried getting off a high roof).

Am I wrong somewhere? Or is this still a bug?

Hello again. :slight_smile:

I found out, that vMenu was blocking this native for me (I think vMenu sets the Invincibility for each frame…). Do you use vMenu? It might be the problem.

EDIT: Forgot to mention this. There is a way around this - not very elegant, but it works. Use the native in a loop. Example:

local isInvincible = false

function toggleInvincible()
    isInvincible = not isInvincible
end

RegisterCommand(
    'godmode',
    toggleInvincible,
    false
)

Citizen.CreateThread(
    function()
        while true do
            Wait(0)
            if isInvincible then
                SetPlayerInvincible(PlayerId(), isInvincible)
            else
                Wait(5000) -- The player isn't invincible, let the thread rest a little :D
            end
        end
    end
)
```    false
)

Citizen.CreateThread(
    function()
        while true do
            Wait(0)
            if isInvincible then
                SetPlayerInvincible(PlayerId(), isInvincible)
            else
                Wait(5000) -- The player isn't invincible, let the thread rest a little :D
            end
        end
    end
)

The better, but more time-consuming solution would be editing the vMenu and compiling it yourself. (You can find the relevant file here)

Thank you for the help!
I’ve tried disabling vMenu and now it works. I’ll try editing the vMenu then