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 6 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:

1local bar = script.Parent
2local player = game.Workspace:WaitForChild(game.Players.LocalPlayer.Name)
3local health = player.Humanoid.health
4 
5while true
6do
7    bar.Size.X.Offset = Vector2.new(health)
8    wait(0.1)
9end

Thanks, blocky010101

3 answers

Log in to vote
0
Answered by 6 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:

1local bar = script.Parent --assuming this is the hp bar
2local plyr = game.Players.LocalPlayer
3local hp = plyr.Humanoid.Health
4 
5hp.Changed:Connect(function()
6    bar.Size = UDim2.new(hp,0, 1, 0)
7end)

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

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

01local bar = script.Parent
02local player = game:GetService("Players")
03local character = player:WaitForChild("Character")
04local health = character:WaitForChild("Humanoid").Health -- Wait for the humanoid to load in.
05 
06while true
07do
08    bar.Size.X.Offset = UDim2.new(health)
09    wait(0.1)
10end

hope this helps.

0
You didn't fixed the problem, for nothing. User#19524 175 — 6y
Log in to vote
-1
Answered by
uhTeddy 101
6 years ago
Edited 6 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:

1local bar = script.Parent
2local player = game:GetService("Players").LocalPlayer
3local health = player.Character:WaitForChild("Humanoid")
4 
5health:GetPropertyChangedSignal("Health"):Connect(function()
6    bar.Size.X.Offset = UDim2.new(health,0,1,0)
7end)

didn't test it hope it works.

Answer this question