Vehicle Health Bar

Hi everyone,

I have been working on making a health bar compatible with my vehicle. I am using a local script that is parented 5 times under where the value is. The value is working, with the health, but my health bar is not tweening down with health. Please help.

Local Script:

local plr = game.Players.LocalPlayer
local char = plr.Character

repeat wait() until char ~= nil

while wait() do
	local life = script.Parent.Parent.Parent.Parent.Parent.Damage.Value
	local f = life/500
	script.Parent:TweenSize(UDim2.new(f,0,0.654,0),"Out","Linear",0.6)
	script.Parent.TextLabel.Text = ""..life.." HP"
end

I did that. It is still not working

why are you tweening the entire frame instead of a child of the frame. if you’re tweeing the entire frame then the backgrounds going to also move with that frame and everything inside it.
change script.parent to script.Parent.Healthbar or whatever you are using as the visual color representation on the display to tween. the tween looks ok, if theirs still a problem then start printing your f variable and making a random frame change its size, if that random frame isn’t changing size then you need to change your tween. should work though.
As for not displaying text label, are you sure this is showing already? you should show how it looks when addressing a visual problem cos i have no idea what the problem is with ur txt label (dk the properties)

bad habits to fix

use heartbeat over “wait()” looping
https://developer.roblox.com/en-us/api-reference/event/RunService/Heartbeat

repeat waiting isnt needed just do
local char = plr.Character or plr.CharacterAdded:Wait()

script.Parent.TextLabel.Text = “”…script.life…" HP"

Try making this into;

script.Parent.TextLabel.Text = “”…life…" HP"

The problem is that GUI’s clone to LocalPlayer.PlayerGui and the parent will be basically nil. You will need to find the car that the player is using with a way or another and then setting the correct parent.

How would I be able to achieve this? I am not cloning the gui anywhere in the script.

Please do not just post links of other topics that don’t relate to the question.

Name an item in your car head, and make sure it is not transparent. Boom. Ease. I did this with a spaceship by simply adding humanoid and then adding a head, so it would take damage.

Oh wait. I see you have a tweening issue instead of a value issue. Allow me a moment.

yes the value is working perfectly fine, but the tween isn’t moving.

@ScriptingsMaster do you need a model with the vehicle?

I don’t need the vehicle at this point.

Is the model in the workspace? I don’t think LocalScripts will work if they aren’t in the StarterGUI (not sure)

Yeah its in the workspace, but I’m pretty sure it should work.

As you can see from this link about LocalScripts, LocalScripts only work in the workspace if it is inside the player’s character. Basically, if it is inside the workspace but not inside the character, then it won’t work.

ok, thanks for that tip. It really helps.

1 Like

Also in your code, you did

local char = plr.Character

repeat wait() until char ~= nil

the 1st line defines the character and the 2nd line waits until the variable is not nil. But you only defined the variable from the start. Basically, if it is nil, the variable will not update when the character is not nil anymore. So your script will wait forever. So what you would do is

local char = plr.Character

repeat
   wait()
   char = plr.Character
until char ~= nil

BUT an easier way to wait until character exists is this:

local char = plr.Character or plr.CharacterAdded:Wait()

Also, I recommend putting the ScreenGui inside StarterGui.