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

Why dose this code only work on Studio?

I made a custom Health Bar. I use the same code in the Roblox Wiki. It did work, but only on studio. Why?

local player = game.Players.LocalPlayer
local Frame = script.Parent
local ScreenGui = script.Parent

Frame.PlayerName.Text = player.Name

local healthBar = ScreenGui.HealthContainer.HealthBar

player.CharacterAdded:connect(function(character)
	local humanoid = character:WaitForChild('Humanoid')
	humanoid.HealthChanged:connect(function(health)
		local healthPercentage = health / character.Humanoid.MaxHealth
		healthBar.Size = UDim2.new(healthPercentage, 0, 1, 0)
	end)
end)

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)

5 comments

1

Make sure this is in a LocalScript

0

It is in the local script

0

Is it in a localscript that is inside StarterPlayerScripts or StarterGui?

0

Its in StarterGui

0

Any errors? I would try waiting for the frame using waitforchild. Also why do you have two variables for the same thing? "Frame" is getting the parent of the script and so is "ScreenGui".

1 answer

At the very beginning of your script, put a wait().

The game client has to load more than just Play Solo mode in Studio, so you need to give it some time to load everything properly.

If wait() doesn't work, put a larger waiting time value in it, such as wait(0.5) or however long you feel it should be for the game to start the script.

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