How to make a infinite health script

Hello Everyone,
I am trying to make a script which gives the player infinite health, i have tried setting the health to (math.huge) and i have tried to set the max health and health to something like 1x10^12 but no luck, the health doesnt change, i want to have this change on the server so damage scripts can run properly, but i have no idea on how to do it properly,

here is the script which applies the infinite health effect

local RS = game:GetService("ReplicatedStorage")
local Event = RS:WaitForChild("ChangePlrStats")


Event.OnServerEvent:Connect(function(player,Health,Hunger,PlrToChange)
	local CurrentPlayer = game.Players:WaitForChild(PlrToChange)
	local Plrstats = CurrentPlayer:WaitForChild("PlayerValues")
	local HungerVal = Plrstats.Hunger
	local plrchar = CurrentPlayer.Character or CurrentPlayer.CharacterAdded:wait()
	local Humanoid = plrchar:WaitForChild("Humanoid")
	local HealthVal = Humanoid.Health
	local MaxHealth = Humanoid.MaxHealth
	
	
	if Hunger == "inf" then
		HungerVal.Value = math.huge
		
	else
		HungerVal.Value = Hunger
	end
	
	
	if Health == "inf" then
		HealthVal = math.huge
		MaxHealth = math.huge

	else
		HealthVal = Health
	end
	
end)

there is also a hunger variable, but ignore this as that works, expect the health value, which doesn’t change at all, neither does the max health value, I just need to know how to set a players health value to a really large number, whenever a player types “inf” in a textbox, if there is any other way to write this script it would be great to know

Any help would be appreciated

3 Likes

You could just set the health to any number, and whenever it is damaged, just not deal damage if you find that it has infinite health (check if a player owns a gamepass, et cetera), and change the text to ‘Infinite’.

ok i will give this a try , thanks for the help!

1 Like

Just add a forcefield to the player’s character. Something like this:

local ff = Instance.new("ForceField")
ff.Parent = plrchar -- Add it to their character.
ff.Visible = false -- makes it invisible so it just seems like they are invincible.
1 Like

ok, i will try this too, thanks for the help

i also have a new issue @synical4 and @ComplexMetatable , the script i am running the code in wont actually change the health, even if i set it to something like 45 I’m very confused on what to do

in this part you need to set max health first then the health

if Health == "inf" then
        MaxHealth = math.huge
		HealthVal = math.huge

yes i have changed that thanks for the help :slight_smile:

It is because you reference the player’s health outside of the part where it is actually changed. This is what I have noticed in my time scripting:

local health = humanoid.Health
health = 50

Well, why wouldn’t this work? Here is why:
The player has their own health. This is a number in the humanoid. When you say humanoid.Health like that, you are just simply getting whatever number the health is at. It doesn’t actually reference the property. You also need to change the max health first if you are going to do this by the way.

Here is the correct code:

Event.OnServerEvent:Connect(function(player,Health,Hunger,PlrToChange)
	local CurrentPlayer = game.Players:WaitForChild(PlrToChange)
	local Plrstats = CurrentPlayer:WaitForChild("PlayerValues")
	local HungerVal = Plrstats.Hunger
	local plrchar = CurrentPlayer.Character or CurrentPlayer.CharacterAdded:wait()
	local Humanoid = plrchar:WaitForChild("Humanoid")
	
	
	if Hunger == "inf" then
		HungerVal.Value = math.huge
		
	else
		HungerVal.Value = Hunger
	end
	
	
	if Health == "inf" then
        Humanoid.MaxHealth = math.huge
		Humanoid.Health = math.huge

	else
		
	end
	
end)

A slight issue I have had in the past with setting health to math.huge is that it doesn’t always work. This is why I recommend an invisible force field. Force fields are more reliable + they protect against explosions breaking joints too.

Simple Trick is to
1.Disable death state of the Humanoid

humanoid:SetStateEnabled(Enum.HumanoidStateType.Death, false)

2.Disable BreakJoints

humanoid.BreakJointsOnDeath = false
  1. Set health to infinity

That works 100% of the time

1 Like

Or you could just use a force field with Visible false. It requires less work and already disabled joint breaking and such from explosions.

Also, why set the death state to false if they aren’t even dead?

1 Like

u still can use Humanoid.Health = 0

1 Like

ah i see, i will try this thanks so much for all the help @kalabgs and @synical4 and @ComplexMetatable and @MrchipsMa :slight_smile:

1 Like

The thing is though isn’t that basically bad practice for damaging humanoids anyway? TakeDamage() works just fine instead of having to do something like:

local dmg = 50
h.Health = h.Health - dmg

Plus, even if the health is set to inf doesn’t mean something else can just set it back to 0 using what you just talked about.

Unless the death state prevents that from happening. If that is the case, ignore this.

1 Like

alright, ive found the solution, the forcefeild idea is very good and i will take note of that but all i needed to know was what @synical4 said, i need to reference the PROPERTY and not the Number, i need to not make mistakes like that, anyway, thanks to everyone for the help and all your responses are valid, thanks again for the help!

1 Like

No problem. I have a feeling that you don’t make this mistake often, because you referenced their hunger property just fine. I’ve done this countless times, so Im glad to help :smile:

2 Likes

sometimes when you code for a extended period of time, all the variables and other stuff just get mixed up and you can spend hours trying to find a simple mistake :smiley:
but yeah, thanks for all the help again

2 Likes