Trying to create a system that relays players health to text but get error

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Title.

  2. What is the issue? Include screenshots / videos if possible!
    I get an error that says “Humanoid is not a valid member of Model “Workspace.cooI_Ginger” - Client - LocalScript:3” For some reason it says the humanoid instance doesn’t exist in my character.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Changing to findfirstchild, tried tostring

local player=game.Players.LocalPlayer
local char=player.Character
local hum=char.Humanoid
while true do
	local health=char.Health
	script.Parent.Text=health
	wait(0.01)
end

Try putting the script in StarterCharacterScripts, because the script has loaded before the character spawns.

1 Like

Disclaimer: This is written on mobile so some things might be off

Better way:

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait() 
local hum = Character:FindFirstChild("Humanoid") or Character:WaitForChild("Humanoid") 

Humanoid:GetPropertyChangedSignal("Health"):connect(function (NewValue) 

script.Parent.Text = tostring(NewValue) 
end) 
1 Like

how would I change the text? I am fairly new to developing so thanks! also same error message came up

Hi cool_Ginger!

I recommend taking a look at the HealthChanged documentation which can be found here: Humanoid | Documentation - Roblox Creator Hub

These are a couple tips/answers that I’d recommend:

  1. Replace the while true do loop
  • While loops are great for some things, but in cases like these where you’re trying to update an object based on changes I’d strongly recommend events. Events are different from while loops because instead of running constantly, they respond to specific actions which in this case would be the humanoid’s HealthChanged event. Overall they’re just more performant. You can learn more about events here: Events | Documentation - Roblox Creator Hub

  • This piece of code to my knowledge would break if the player resets or respawns. This is because when the player respawns, the game generates a new character for the player. The old value of the Char variable would hold the value of Nil which is not what you want in this case. The HealthChanged documentation I linked shows how you could use the .CharacterAdded event to correct this error to make this script functional. You can learn more about .CharacterAdded here: Player | Documentation - Roblox Creator Hub

  1. Replace wait() with task.wait()
  1. How do you convert the current health(integer) into text(string)?
  1. How would I change the text?
  • You had the right concept of indexing the .Text property so good job on that part. You’d just need to convert health into a text.

Old code:

script.Parent.Text=health

New code:

script.Parent.Text = tostring(health)

Best of luck with developing and don’t be afraid to ask any questions.

Note: If I shared any misinformation or if there’s a better way of doing something please educate me. Thanks!

1 Like

My code still doesn’t work and even if the issue is that the humanoid health never changes, I have it so the player instantly takes 1 damage when you load in.

local playerService=game.Players
playerService.PlayerAdded:Connect(function(plr)
	print('playerAdded')
	plr.CharacterAdded:Connect(function(char)
		print('charAdded')
		local hum=char.Humanoid
		hum.HealthChanged:Connect(function(health)
			local ui=script.Parent
			ui.Text=tostring(health)
			
		end)
	end)
end```

Thats because gameJoined event doesn’t work on client

Where is this script located?

Open the output on the top, View > Output. Read the red messages and fix your code.

If you need any help with the error, send the code it errored in and the error itself.