Local Script is not running code

Hello!

I am trying to create a health bar. I have a ScreenGui with all the gui elements and a local script inside of it which has the following code:

--Ill put comments so Adalaar if youre reading this you can better understand the code


--Defining important variables
local lPlayer = game:GetService("Players").LocalPlayer -- The player
local plrChar -- The character (Not defined so it doesnt error while loading in the new one)
local plrHumanoid -- The humanoid which has the health and maxhealth
local hbBar = script.Parent:WaitForChild("HEALTH")

-- Health bar variables
local hbDivision -- How many bars is the hBar divided into


local hbDefaultSize = UDim2.new(0, 346, 0, 26)
local hbPosition = UDim2.new(0.015, 0, 0.873, 0) -- May change on screen size

-- Tile size: {0, 10},{0, 26}


lPlayer.CharacterAdded:Connect(function(char)
	print("Player Char added")
	plrChar = char
	plrHumanoid = char:WaitForChild("Humanoid") -- setting the values
	hbBar.Size = hbDefaultSize
end)

For some reason it is not printing the β€œPlayer Char added” so I know the code is not running? Does anyone know why the code doesn’t run when the player character is added?

1 Like

Where is the Local Script located?

1 Like

Read the 2nd sentence.​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

i sometimes miss some stuff, sorry

1 Like

The Local script is located in the Screen Gui

I think that guis (probably) load after the player loads?

??? I just define plrChar as what CharacterAdded passes

Ill try to define the character as already existing

That message wasn’t meant for you, but anyways try this:

--Ill put comments so Adalaar if youre reading this you can better understand the code


--Defining important variables
local lPlayer = game:GetService("Players").LocalPlayer -- The player
local plrChar = lPlayer.Character or lPlayer.CharacterAdded:Wait()
local plrHumanoid = plrChar:WaitForChild("Humanoid")
local hbBar = script.Parent:WaitForChild("HEALTH")

-- Health bar variables
local hbDivision -- How many bars is the hBar divided into


local hbDefaultSize = UDim2.new(0, 346, 0, 26)
local hbPosition = UDim2.new(0.015, 0, 0.873, 0) -- May change on screen size

-- Tile size: {0, 10},{0, 26}

hbBar.Size = hbDefaultSize
1 Like

Okay thats probably it because I dont think a new character model loads in after you die

Make sure your screenGui has the resetOnRespawn (it’s called something like that) property enabled as well, I’m pretty sure this wont work without it or you could just modify the script a bit and move it into starterPlayerScripts.

Okay thanks and God bless you it works now

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.