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

Why does my health gui script only work in studio (none of it works in game at all)?

Asked by 8 years ago
Edited 8 years ago

I made a health gui on my own for practice which works perfectly in studio but not in game. I have a textlabel that should display the player's health, change size (should be twice the plr's health in the second of the 4 numbers in the UDim2, and changes color. It works in both local scripts and normal scripts in studio but neither in game.

Image in game: http://imgur.com/VkaZY4s

studio video: https://www.youtube.com/watch?v=J9Q0zF4dVdM&feature=youtu.be

script:

plr = game.Players.LocalPlayer
hum = plr.Character:FindFirstChild('Humanoid') -- finding the player's humanoid

local green = BrickColor.new(304) -- green color for 80+ health
good = green.Color
print(good)

local orange = BrickColor.new(38) -- orange color for 30 - 79 health
ok = orange.Color
print(ok)

local red = BrickColor.new(327) -- red color for 1 - 29 health
bad = red.Color
print(bad)

i = false
while i ~= true do --inf loop so the gui is almost always accurate
    wait(.01)
    goo = math.floor(hum.Health) * 2  -- plr's health floor *2 used for gui size
    script.Parent.TextLabel.Size = UDim2.new(0, goo, 0, 15) -- change gui size
    script.Parent.TextLabel.Text = goo/2 -- display plr's health
    if goo/2 > 79 then -- determining if color should be green
        script.Parent.TextLabel.BackgroundColor3 = good
    end
    if goo/2 < 80 and goo/2 > 30 then -- determining if color should be orange
        script.Parent.TextLabel.BackgroundColor3 = ok
    end
    if goo/2 < 30 then -- determining if color should be red
        script.Parent.TextLabel.BackgroundColor3 = bad
    end
end

0
Probably because you're not waiting for things to load. User#11440 120 — 8y
0
good point MrDefaultMan 113 — 8y

1 answer

Log in to vote
0
Answered by
guiraz 25
8 years ago

exactly, create variables for the interface elements using script.Parent:WaitForChild("ElementName")

0
I tried that but later realized that I should have done the entire thing in a plr.Humanoid.HealthChanged function. MrDefaultMan 113 — 8y
Ad

Answer this question