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

Text doesn't change if health is below 90?

Asked by 5 years ago

I have got this script so that if the players health is below...whatever number it is in the script then a gui text will change.But it never works and i always get an error saying "Attempt to index local Human (a nil value)" please help.

CODE:

local PainText = script.Parent --The text that will show the players pain
local Human = script:FindFirstChild("Humanoid")--to detect the health for each type of pain

--Code Here
    if Human.Health < 90 then
        PainText.Text = "BRUISED"
    elseif Human.Health > 90 then
        PainText.Text = "HEALTHY"
end
0
Change Human. The Humanoid isn't going to be inside the script Sergiomontani10 236 — 5y
0
its a local script RoccoAttackYT 8 — 5y

3 answers

Log in to vote
0
Answered by
lunatic5 409 Moderation Voter
5 years ago
Edited 5 years ago

I believe your problem here is that the script only checks the player's health once. What you need is a Changed event. Also, you should try implementing what thesit123 said in his answer as well, as that is another problem you have with your script. Basically, a Changed event is fired whenever a property of an Instance is changed. In your case, you want to check the health of the player, so your script would read something like this:

local PainText = script.Parent --The text that will show the players pain
local Human = script:FindFirstChild("Humanoid")

Human.Changed:Connect(function()
    if Human.Health < 90 then
        PainText.Text = "BRUISED"
    elseif Human.Health > 90 then
        PainText.Text = "HEALTHY"
    end
end)

If you need more information on Changed events, visit this ROBLOX Wiki page: http://wiki.roblox.com/index.php?title=API:Class/Instance/Changed

Hope this helps! If it does, please accept my answer!

EDIT: I just realized that incapaz answered the question before I did while I was typing the answer. So technically, he answered before I did. He also used a function I haven't even heard of, which is most likely more efficient than the one I used. Well played, man. Well played.

0
Thanks man.This worked! RoccoAttackYT 8 — 5y
0
No problem! Glad to help! lunatic5 409 — 5y
Ad
Log in to vote
1
Answered by 5 years ago

It doesn’t work, because you’re trying to find the humanoid inside the script. You find it using Player.Character. You use a Changed event to check if the health changed.

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char.Humanoid

hum.HealthChanged:Connect(function(newHealth)
    if newHealth > 90 then
        script.Parent.Text = "HEALTHY"
    else
        script.Parent.Text = "BRUISED"
    end
end)

HealthChanged fires when the Health changes. As a parameter, it passes the new health, after the change.

Log in to vote
0
Answered by
thesit123 509 Moderation Voter
5 years ago

What you are trying to do is getting a humanoid from player that won't ever exist.

Get the LocalPlayer: local player = game.Players.LocalPlayer

Get the Character: local character = Player.Character or Player.CharacterAdded:wait()

Wait for Humanoid : local human = character:WaitForChild("Humanoid")

If you follow all of that you should have something like this:

local PainText = script.Parent --The text that will show the players pain
local player = game.Players.LocalPlayer
local character  = Player.Character or Player.CharacterAdded:wait()
local Human = character:WaitForChild("Humanoid") --to detect the health for each type of pain

--Code Here
    if Human.Health < 90 then
        PainText.Text = "BRUISED"
    elseif Human.Health > 90 then
        PainText.Text = "HEALTHY"
end
0
Ok it works but the text doesn't change RoccoAttackYT 8 — 5y
0
Then, it might be your other codes. thesit123 509 — 5y
0
no its definetley not my other codes.i haven't received errors either RoccoAttackYT 8 — 5y

Answer this question