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

Can't get humanoid in a local script to show my health in gui, (?)

Asked by 4 years ago
Edited 4 years ago

That's my script :

local Players = game:GetService("Players").LocalPlayer local Character = Players:FindFirstChild("Character") local Humanoid = Character:FindFirstChild("Humanoid")

function Health() script.Parent.Text = tostring(Humanoid.Health.."Health") end Health() Humanoid.Health.Changed:Connect(Health)

That's basically my script somewhy it doesn't work, Thanks for helping

0
It will be helpful if you keep the script in Lua block code BestCreativeBoy 1395 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Well, there were few flaws in your code. I edited all of them and it might work. Comments are also added what's wrong.

local Players = game:GetService("Players").LocalPlayer 
-- local Character = Players:FindFirstChild("Character") Well, don't just find Character of the player, the Character takes some time to load
repeat wait() until Players.Character -- Waits for the Character to load completely
local Humanoid = Players.Character:FindFirstChild("Humanoid")

function Health() 
    script.Parent.Text = tostring(Humanoid.Health.." Health") 
end 

Health() 

-- Humanoid.Health.Changed:Connect(Health) Don't use Health.Changed, roblox have in-built function to detect the Change in Health, it is called 'HealthChanged'
Humanoid.HealthChanged:Connect(Health)

Lemme know if it helps!

1
Thx AmirGavron -2 — 4y
0
It will be helpful if you mark it as the answer BestCreativeBoy 1395 — 4y
Ad

Answer this question