Seems to be a bug, changing the Health property after setting MaxHealth to something lower automatically clamps it and sets it to the new MaxHealth.
Basically, if you set MaxHealth to 50 and then set Health to 1000 from 100, then it’ll automatically go down to 50, and for some reason act like you took damage and show the red damage effect.
I think this is more of an discussion topic than a bug, the Health Script has remained unchanged for a large amount of time, and doesn’t handle this issue. A temporary solution would be to add this block of code above the health script while loop:
--The Health script can be located under the player character, either fork it, or create a new script for handling this issue
Humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(function()
local max = Humanoid.MaxHealth
if Humanoid.Health > max then
Humanoid.Health = max
end
end)
PS: Personally, I consider the Health script outdated, it must be updated for efficiency and issues like this.
This seems a little long-winded, you could just write a wrapper which changes both properties at once or just explicitly assign each property when necessary.
local function changeHealth(humanoid, health)
humanoid.MaxHealth = health
humanoid.Health = health
end
It depends on what behavior you’re trying to achieve, I was under the impression that the thread’s poster wanted them to be changed in tandem with one another. It’s a relatively simple change.
local function changeHealth(humanoid, health)
humanoid.MaxHealth = health
humanoid.Health = if health > humanoid.Health then humanoid.Health else health
end
if you need context as to why urs doesnt work :
food poisoning reduces the max health, to 80 you can get food poisoning when your health is less than the max so i dont want to increase the health to 80 rather reduce it if its over the max health.