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

Why is my GUI health bar I created not working?

Asked by 5 years ago

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

Log in to vote
0
Answered by 5 years ago

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.

0
plyr:WaitForChild("Humanoid").Health i recomend. AltNature 169 — 5y
0
That's still wrong. Numbers don't have changed events. And the Humanoid is in the character, not the player. User#19524 175 — 5y
0
sorry im late, does not work. blocky010101 23 — 5y
Ad
Log in to vote
0
Answered by
AltNature 169
5 years ago
Edited 5 years ago

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.

0
You didn't fixed the problem, for nothing. User#19524 175 — 5y
Log in to vote
-1
Answered by
uhTeddy 101
5 years ago
Edited 5 years ago

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.

Answer this question