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

My health bar script works in studio testing, but not in the actual game?

Asked by 5 years ago
Edited 5 years ago

I'm trying to make a game and I made a health bar. I've maid health bars before and normally this isn't a problem. I tested it in studio and it worked just like intended, but when I tested it in the actual game on roblox, it didn't work. Script:

local healthgui = script.Parent
local frame = healthgui:WaitForChild("Frame")
local number = healthgui:WaitForChild("Number")
local player = game.Players.LocalPlayer
local char = player.Character
local healthstat = player:WaitForChild("Health")
local maxstat = player:WaitForChild("MaxHealth")
local words = healthgui:WaitForChild("Health")

local green = Color3.new(0,255,0)
local red = Color3.new(255,0,0)
local yellow = Color3.new(255,255,0)

char.Humanoid.Changed:connect(function()
    local health = char:WaitForChild("Humanoid").Health
    local maxhealth = char:WaitForChild("Humanoid").MaxHealth
    local redzone = maxhealth*0.25
    local yellowzone = maxhealth*0.5
    if health <= redzone then
        frame.BackgroundColor3 = red
        number.TextStrokeColor3 = red
        words.TextStrokeColor3 = red
    elseif health <= yellowzone then
        frame.BackgroundColor3 = yellow
        number.TextStrokeColor3 = yellow
        words.TextStrokeColor3 = yellow
    else
        frame.BackgroundColor3 = green
        number.TextStrokeColor3 = green
        words.TextStrokeColor3 = green
    end
    frame:TweenSize(UDim2.new(healthstat.Value/maxstat.Value*0.93,0,0,24),"Out","Quart",1)
    number.Text = healthstat.Value.."/"..maxstat.Value
end)

while wait() do
    local level = player:WaitForChild("leaderstats"):WaitForChild("Level")
    if player.Name == "Blue_Deku19" then
        char:WaitForChild("Humanoid").MaxHealth = (level.Value * 5 + 95)*5
        healthstat.Value = char.Humanoid.Health/5
        maxstat.Value = char.Humanoid.MaxHealth/5
    else
        char:WaitForChild("Humanoid").MaxHealth = level.Value * 5 + 95
        healthstat.Value = char.Humanoid.Health
        maxstat.Value = char.Humanoid.MaxHealth
    end
end

I tested which part it fails at by making it make a part wherever your current player is and I moved that around, it didn't even make the variables in the first part. This is a local script in StarterGui, does anyone know what's wrong with it? Sorry if it's long e.e. Also, it showed no problems whatsoever in the output when I was testing it in studio.

0
its probably the wait for childs... since u have so many, it causes and infinite yield and to simply fix it, go to the console in game mode, find the infinite yield and simple change it in studii greatneil80 2647 — 5y
0
if there was an infinite yeild problem, wouldn't the problem appear in output in studio testing? and infinite yeild only happens when the waitforchild hasn't found the child for a while i think Knineteen19 307 — 5y

1 answer

Log in to vote
-1
Answered by 5 years ago

It seems that it can possibly be one or more of your waitforchilds, check for spelling errors

0
if it was a spelling error, it would've showed something in output when i was testing it in studio, but in studio, it works without a problem, so that couldn't be it. Knineteen19 307 — 5y
Ad

Answer this question