I made a function that makes my custom health bar work, But when I run this script it says "Attempt to connect failed: Passed value is not a function" Please help!
function barFunction(plr) -- makes my custom health bar work
local gui = plr.PlayerGui:WaitForChild("Health", 99)
local hbar = gui:WaitForChild("Bar", 99)
local playerhp = gui:WaitForChild("PlayerHealth", 99)
local maxhealth = gui:WaitForChild("MaxHealth", 99)
local percent = playerhp.Value / maxhealth.Value
playerhp.Changed:connect(function()
if not (playerhp.Value <= 0) then
--//color
local green = percent
local red = 1 - green
hbar.BackgroundColor3 = Color3.new(red,green,0)
--//size
hbar.Size = UDim2.new(0,percent * x,0,y)
end
end)
end
game.Players.PlayerAdded:Connect(function(plr) --
--//character check
repeat wait() until plr.Character
plr.CharacterAdded:connect(barFunction(plr))
end)

4 comments
But every time the player dies, I have to reload the changed event again, Thats why I did it, Without doing it, The changed event doesn't work
@sahar1213 why dont you declare the variables at the beginning of the script and just use the changed event listener?
@aazkao because when the player dies, for some reason the playerhp regens and i have to declare it again and do the changed function again.
Can somebody give a good awnser please?