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!
01 | function barFunction(plr) -- makes my custom health bar work |
02 | local gui = plr.PlayerGui:WaitForChild( "Health" , 99 ) |
03 | local hbar = gui:WaitForChild( "Bar" , 99 ) |
04 | local playerhp = gui:WaitForChild( "PlayerHealth" , 99 ) |
05 | local maxhealth = gui:WaitForChild( "MaxHealth" , 99 ) |
06 | local percent = playerhp.Value / maxhealth.Value |
07 | playerhp.Changed:connect( function () |
08 | if not (playerhp.Value < = 0 ) then |
09 | --//color |
10 | local green = percent |
11 | local red = 1 - green |
12 | hbar.BackgroundColor 3 = Color 3. new(red,green, 0 ) |
13 | --//size |
14 | hbar.Size = UDim 2. new( 0 ,percent * x, 0 ,y) |
15 | 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.
01 | function barFunction(plr) -- makes my custom health bar work |
02 | plr.CharacterAdded:Wait() -- Ensure the character exists |
03 | local gui = plr.PlayerGui:WaitForChild( "Health" , 99 ) |
04 | local hbar = gui:WaitForChild( "Bar" , 99 ) |
05 | local playerhp = gui:WaitForChild( "PlayerHealth" , 99 ) |
06 | local maxhealth = gui:WaitForChild( "MaxHealth" , 99 ) |
07 | local percent = playerhp.Value / maxhealth.Value |
08 | playerhp.Changed:connect( function () |
09 | if not (playerhp.Value < = 0 ) then |
10 | --//color |
11 | local green = percent |
12 | local red = 1 - green |
13 | hbar.BackgroundColor 3 = Color 3. new(red,green, 0 ) |
14 | --//size |
15 | hbar.Size = UDim 2. new( 0 ,percent * x, 0 ,y) |