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

Why is my GUI health bar I created not working?

I have created a health bar GUI in the StarterGui, but the script is not working. The LocalScript is inside the frame that changes size. In the output in says "Offset cannot be assigned to" . :(

LocalScript:

local bar = script.Parent
local player = game.Workspace:WaitForChild(game.Players.LocalPlayer.Name)
local health = player.Humanoid.health

while true
do
	bar.Size.X.Offset = Vector2.new(health)
	wait(0.1)
end

Thanks, blocky010101

3 answers

You shouldn't use Vector2 when creating HP bars (or GUIs, i don't recommend it) but rather use UDim2 and for the LocalPlayer you don't need a name. Here's a basic example of what you should do:

local bar = script.Parent --assuming this is the hp bar
local plyr = game.Players.LocalPlayer
local hp = plyr.Humanoid.Health

hp.Changed:Connect(function()
	bar.Size = UDim2.new(hp,0, 1, 0)
end)

Hopefully this helps and tell me if you get any errors.

3 comments

0

plyr:WaitForChild("Humanoid").Health i recomend.

0

That's still wrong. Numbers don't have changed events. And the Humanoid is in the character, not the player.

0

sorry im late, does not work.

Your telling the server that the variable Player is the name of the player, which is fine, until you tell the server that the variable health is the name of the player dot humanoid dot health, Which doesn't make sense. So just take away dot name on line 2, and use UDim2.new for line 7.

the server even tells you offset cannot be assigned to.

local bar = script.Parent
local player = game:GetService("Players") 
local character = player:WaitForChild("Character")
local health = character:WaitForChild("Humanoid").Health -- Wait for the humanoid to load in.

while true
do
    bar.Size.X.Offset = UDim2.new(health)
    wait(0.1)
end

hope this helps.

1 comment

0

You didn't fixed the problem, for nothing.

Okay I'm not gonna explain just gonna give you a working script because everyone else is getting the Humanoid from the player not the character.

LOCALSCRIPT:

local bar = script.Parent
local player = game:GetService("Players").LocalPlayer
local health = player.Character:WaitForChild("Humanoid")

health:GetPropertyChangedSignal("Health"):Connect(function()
    bar.Size.X.Offset = UDim2.new(health,0,1,0)
end)

didn't test it hope it works.

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