How to make player invincible

As the title suggest, I am making a debug menu and my first button is to make the player invulnerable to all sources of damage.

I have tried giving the player a force field, however when I place a part that takes away the players health it still damages and kills the player.

Additionally I have tried simply setting the player health really big and constantly restoring the health, however that means the script will run constantly and I want to find an alternative.

local character = player.Character

function pressed ()
	local FF = Instance.new("ForceField", character)
end

invulnButton.MouseButton1Click:Connect(pressed)

After some research I found out the force field may only act as a way to prevent the player from exploding.

So what alternative should I use to prevent any damage being given?

2 Likes

Sorry if I wasn’t clear enough :stuck_out_tongue:

What i meant was the player would take damage and would not get it restored (since the player regen is disabled for game play) without using a loop function.

For example:

while bigHealth == true do
   Player.Humanoid.Health == max
   wait(however long)
end

I would prefer not need to call back to this script at all in this regard.

1 Like

Disable the humanoid dead state? Also, forcefields only negates damage through Humanoid:TakeDamage, so it won’t prevent damage that manipulates the health directly.

1 Like

Just tested it out and the player still takes damage from Humanoid:TakeDamage.

How would you handle it? I might be using it wrong :stuck_out_tongue:

1 Like

Are you sure you’re doing Humanoid:TakeDamage(99)
and follows one of these conditions :

  • The ForceField shares the same Instance/Parent as the Humanoid
  • The ForceField is parented to the Humanoid/RootPart of the Humanoid
  • The ForceField is parented to an ancestor of the Humanoid other than the Workspace
1 Like

Would this happen to satisfy the requirements?

function onTouched(hit)
	script.Parent.Sound:play()
	hit.Parent:findFirstChild("Humanoid"):TakeDamage(99) 
end
script.Parent.Touched:connect(onTouched)

image

1 Like

Are you creating the forcefield on the server or the client? From the first post it seems you are creating it from the client. You should fire a remote and create the forcefield on the server.

Because the part damages from the server it cannot see the locally created forcefield.

Just use the HealthChanged event on Humanoid, if you want the health to floor on 1.

Humanoid.HealthChanged:Connect(function(health)
    if health < 1 then
        Humanoid.Health = 1
    end
end)

-- or permanent

Humanoid.HealthChanged:Connect(function()
    Humanoid.Health = 100 -- or Humanoid.MaxHealth
end)

ForceField is optional, and it can toggle visibility.

1 Like

It seemed to work fine, however the player still ended up re spawning even if the player didn’t ‘die’?

Any way to combat that?

I almost forgot to ask, is this done through a server script? If not, do that. In order to work with it from a LocalScript, proceed firing a RemoteEvent.

4 Likes

It seems my troubles stem from my use of local scripts :stuck_out_tongue:

I’ll have a look into how to fire remove events and we’ll see from there!