You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
Title.
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.
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
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)
These are a couple tips/answers that I’d recommend:
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
Replace wait() with task.wait()
In short, task.wait() is just an improved version of the wait function. You can learn more in depth about the why’s and how’s here: task | Documentation - Roblox Creator Hub
How do you convert the current health(integer) into text(string)?
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```