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)
You can not use arguments when connecting. These are passed with the event. Don't worry, the plr argument is returned by CharacterAdded.
Plus, I removed some useless things from your script, that served no purpose but making the code more complex to read.
You can use game.Players:GetPlayerFromCharacter()
to get the player.
function barFunction(plr) -- makes my custom health bar work plr.CharacterAdded:Wait() -- Ensure the character exists 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(barFunction) -- Simplify