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

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

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

2 comments

0

Probably because you're not waiting for things to load.

0

good point

1 answer

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

1 comment

0

I tried that but later realized that I should have done the entire thing in a plr.Humanoid.HealthChanged function.

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