Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why doesn't my health GUI script work in game when it works perfectly fine in studio?

I don't know whats happening, I made multiple local scripts, all in areas that connect to the client (starter pack, starter gui, starter player scripts), each script works perfectly fine in studio, but they don't seem to work in game. Below is one of those scripts. Why doesn't this work?

line 1 currently returns an error - infinite yield possible on "LocalPlayer"

local char = game.Players:WaitForChild("LocalPlayer")
local hum = char:WaitForChild("Character").Humanoid

local bar = script.Parent.HealthBar
local border = script.Parent

local lowHealth = hum.MaxHealth/4
local fullHealth = hum.MaxHealth

local healthtxt = script.Parent.Parent.HealthPercentage


hum.Changed:Connect(function()
	
	local HP = hum.Health / hum.MaxHealth
	local roundedHP = math.floor(hum.Health + 0.5)
	
	healthtxt.Text = roundedHP .."%"
	
	if roundedHP <= lowHealth then
		healthtxt.TextColor3 = Color3.fromRGB(211,0,0)
	elseif roundedHP == 100 then
		healthtxt.TextColor3 = Color3.fromRGB(0,255,0)
	else
		healthtxt.TextColor3 = Color3.fromRGB(255,255,255)
		
	end
	
	if hum.Health > lowHealth then
		bar.BackgroundColor3 = Color3.fromRGB(255,255,255)
		border.BackgroundColor3 = Color3.fromRGB(33,33,33)
	else
		bar.BackgroundColor3 = Color3.fromRGB(211,0,0)
		border.BackgroundColor3 = Color3.fromRGB(89,29,29)
	end
	bar.Size = UDim2.new(HP,0,1,0)

end)

3 comments

0

just use game.Players.LocalPlayer instead of waiting

0

yeah i did that before, but thats when i first got the error. I switched to this to see if that fixed the problem

0

Actually WaitForChild("LocalPlayer") would wait for an object called LocalPlayer

1 answer

✓ Accepted answer

###This is happening because LocalPlayer is a property of Players, not a child of Players. Properties are not children of Instances. It can be (not the property itself) if the property holds an object reference and that object can be the child of the Instance. To fix that, remove the WaitForChilds. If you truly wanted to wait for the character, use the Player.CharacterAdded event.

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
-- You can't assume the character has already been loaded, but neither can you assume it hasn't 

local bar = script.Parent.HealthBar
local border = script.Parent

local lowHealth = hum.MaxHealth/4
local fullHealth = hum.MaxHealth

local healthtxt = script.Parent.Parent.HealthPercentage


hum.HealthChanged:Connect(function(newHealth)

    local HP = newHealth / hum.MaxHealth
    local roundedHP = math.floor(newHealth + 0.5)

    healthtxt.Text = roundedHP .."%"

    if roundedHP <= lowHealth then
        healthtxt.TextColor3 = Color3.fromRGB(211,0,0)
    elseif roundedHP == 100 then
        healthtxt.TextColor3 = Color3.fromRGB(0,255,0)
    else
        healthtxt.TextColor3 = Color3.fromRGB(255,255,255)

    end

    if newHealth> lowHealth then
        bar.BackgroundColor3 = Color3.fromRGB(255,255,255)
        border.BackgroundColor3 = Color3.fromRGB(33,33,33)
    else
        bar.BackgroundColor3 = Color3.fromRGB(211,0,0)
        border.BackgroundColor3 = Color3.fromRGB(89,29,29)
    end

    bar.Size = UDim2.new(HP,0,1,0)

end)


This discussion is preserved as it was. New answers, comments, and votes are closed.