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)
#Reference vs Value
It is important that you know the difference between a reference and a value.
##Reference
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
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.
##Value
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)
print(bool1_val)
Even though _string_value was set to _string, both variables have different memory and modifying one value does not change the other.
3 comments
Change the health with Server, not client. and you can't change player/player character name
Who said OP was changing the player's name
im changing the name of an NPC through a server script