Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

health display script doesn't work, no errors in console?

Asked by 6 years ago

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

0
Change the health with Server, not client. and you can't change player/player character name yHasteeD 1819 — 6y
0
Who said OP was changing the player's name User#24403 69 — 6y
0
im changing the name of an NPC through a server script starwars5251977 48 — 6y

2 answers

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

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

--[[
   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.

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) --> 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.

0
ty starwars5251977 48 — 6y
Ad
Log in to vote
4
Answered by 6 years ago
Edited 5 years ago

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)
0
when health changed can new health be the new max health? HappyTimIsHim 652 — 6y

Answer this question