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:

1local humanoid = script.Parent.Humanoid
2local health = humanoid.Health
3local maxHealth = humanoid.MaxHealth
4 
5script.Parent.Name = health.."/"..maxHealth
6 
7script.Parent.Humanoid.HealthChanged:Connect(function()
8    script.Parent.Name = health.."/"..maxHealth
9end)

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.

1local humanoid = script.Parent.Humanoid
2 
3script.Parent.Name = humanoid.Health.."/"..humanoid.MaxHealth
4 
5script.Parent.Humanoid.HealthChanged:Connect(function(health)
6    script.Parent.Name = health.."/"..humanoid.MaxHealth
7end)

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.

01local tbl1 = {"hello", "world", "foo", "bar"}
02local tbl2 = {"hello", "world", "foo", "bar"}
03local tbl1reference = tbl1
04table.insert(tbl1, "test")
05 
06for _, v in ipairs({tbl1, tbl2, tbl1reference}) do
07    print(table.concat(v, ", "))
08end
09 
10--[[
11   output:
12   hello, world, foo, bar, test
13   hello, world, foo, bar
14   hello, world, foo, bar, test
15 
16--]]

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.

01local _string = "hello"
02local _string_value = _string
03_string = "world"
04print(_string_value)
05 
06local bool1 = false
07local bool1_val = bool1
08bool1 = true
09print(bool1) --> true
10print(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

1local humanoid = script.Parent.Humanoid
2local health = humanoid.Health
3local maxHealth = humanoid.MaxHealth
4 
5script.Parent.Name = health.."/"..maxHealth
6 
7script.Parent.Humanoid.HealthChanged:Connect(function(newHealth)
8    script.Parent.Name = newHealth.."/"..maxHealth
9end)

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

1local humanoid = script.Parent.Humanoid
2local health = humanoid.Health
3local maxHealth = humanoid.MaxHealth
4 
5script.Parent.Name = health.."/"..maxHealth
6 
7script.Parent.Humanoid.HealthChanged:Connect(function(newHealth)
8    script.Parent.Name = newHealth.."/"..humanoid.MaxHealth
9end)
0
when health changed can new health be the new max health? HappyTimIsHim 652 — 6y

Answer this question