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
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â.
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.
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
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.
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.
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!
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
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
but yeah, thanks for all the help again