here is the script:
local humanoid = script.Parent.Humanoid local health = humanoid.Health local maxHealth = humanoid.MaxHealth script.Parent.Name = health.."/"..maxHealth script.Parent.Humanoid.HealthChanged:Connect(function() script.Parent.Name = health.."/"..maxHealth end)
im trying to make the name of the model say how much health it has left in this format:
health / maxhealth
it does not update and shows no errors in the console
The problem here is the fact that you wrongly assume that the health
variable is a reference to the property of the Humanoid
. When the property changes, your health variable will not update. HealthChanged passes the new health as a parameter, so use that instead.
local humanoid = script.Parent.Humanoid script.Parent.Name = humanoid.Health.."/"..humanoid.MaxHealth script.Parent.Humanoid.HealthChanged:Connect(function(health) script.Parent.Name = health.."/"..humanoid.MaxHealth end)
It is important that you know the difference between a reference
and a value
.
In Lua, for example, tables are passed by reference.
local tbl1 = {"hello", "world", "foo", "bar"} local tbl2 = {"hello", "world", "foo", "bar"} local tbl1reference = tbl1 table.insert(tbl1, "test") for _, v in ipairs({tbl1, tbl2, tbl1reference}) do print(table.concat(v, ", ")) end --[[ output: hello, world, foo, bar, test hello, world, foo, bar hello, world, foo, bar, test --]]
tbl1 and tbl1reference hold the same exact table in memory and tbl2 does not, even if it has the same values in the same positions. Changes made to tbl1 or tbl1reference will affect tbl1 and tbl1reference.
However things like booleans and numbers are passed simply via value.
local _string = "hello" local _string_value = _string _string = "world" print(_string_value) local bool1 = false local bool1_val = bool1 bool1 = true print(bool1) --> true print(bool1_val) --> false
Even though _string_value
was set to _string
, both variables have different memory and modifying one value does not change the other.
Well, the problem here seems to be that the health variable isn't changed when the health of the humanoid changes, which means the script is still using the original health of the humanoid rather than the current health of the humanoid.
Luckily the health changed event has a handy argument, the new health , which can be used for this purpose
local humanoid = script.Parent.Humanoid local health = humanoid.Health local maxHealth = humanoid.MaxHealth script.Parent.Name = health.."/"..maxHealth script.Parent.Humanoid.HealthChanged:Connect(function(newHealth) script.Parent.Name = newHealth.."/"..maxHealth end)
Addition: if you plan on also changing the max health of the player, you would also have to use an updated version of the maxHealth variable
local humanoid = script.Parent.Humanoid local health = humanoid.Health local maxHealth = humanoid.MaxHealth script.Parent.Name = health.."/"..maxHealth script.Parent.Humanoid.HealthChanged:Connect(function(newHealth) script.Parent.Name = newHealth.."/"..humanoid.MaxHealth end)