How to make an Invincibility script

I am making an obby and players can buy an invincibility power-up that stops them from dying on kill parts. I tried giving players a forcefield and changing their max health and health to math.huge but neither of those have worked.

part.Touched:Connect(function(hit)
	local human = hit.Parent:FindFirstChild("Humanoid")
	if human ~= nil then
		human.Health = 0
	end
end)

This is the script inside of the kill parts. I am aware if I change this script it would make things a lot easier but this script is already inside probably a couple hundred kill parts and would rather not go through and delete all of them.

3 Likes

Well i quickly drew this up but it should do the job

In the invincibility giver code, put this line to set their invincibility:

player:SetAttribute("Invincible", true)
 -- Make sure that you change "player" to whatever your value for it is

In the script you provided, change it to this:

part.Touched:Connect(function(hit)
	local human = hit.Parent:FindFirstChild("Humanoid")
	if human then
		local player = game.Players:GetPlayerFromCharacter(human.Parent)
		
		if player then
			if player:GetAttribute("Invincible") then return end -- checks to see if this attribute is true, if it is, it wont kill the player
			human.Health = 0
		end
	end
end)

Remember to set the attribute to false after the perk expires, or else the invincibility will last forever.

4 Likes

I tested this out and it didn’t work, even when the player had the invincible attribute the part couldn’t recognize it and still killed the player.

You’re likely setting the attribute with a localscript. Remember, stuff done on a localscript won’t be replicated to the server unless you’re using RemoteEvents/Functions!

True invincibility requires Humanoid.HealthChanged event to be connected to a function. If the health were to go below 0, set it back to 1 or a number that is not 0.

Knowing this, you’ll need to set a function for Humanoid.HealthChanged that has a check if the player is invincible or not. If it’s not invincible, do nothing. You’d most likely utilize the attributes to add to the character for the conditional.

2 Likes

Use Humanoid:TakeDamage() to damage Players instead of setting it to 0.

Also it would make more sense to loop through all kill bricks in one script and set the Touched event there since the method you are using is bad for organization.

I set the attribute using the command bar which is server sided.

1 Like

I suggest using human:TakeDamage() Because humanoid:TakeDamage() respects forcefield in other words humanoid:TakeDamage() doesnt work if player has a forcefield and then put a detect if player bought the Invisibility you need to put a ForceField to the player and set the it to ForceField.Visible = false - so that the ForceField effect would’nt appear. And also the ForceField Position must be put to the Character of the player who bought the invinsibility.

3 Likes

Also you can just check if player have gamepass, and if he is just don’t deal him any damage, and yeah, i know that i answered after 1 and half year.