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
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.
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.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