Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Attempt to connect failed: Passed value is not a function Why doesn't this work?

Asked by 6 years ago

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!

01function 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.BackgroundColor3 = Color3.new(red,green,0)
13        --//size
14        hbar.Size = UDim2.new(0,percent * x,0,y)
15        end
View all 24 lines...

1 answer

Log in to vote
1
Answered by
Aimarekin 345 Moderation Voter
6 years ago
Edited 6 years ago

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.

01function 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.BackgroundColor3 = Color3.new(red,green,0)
14        --//size
15        hbar.Size = UDim2.new(0,percent * x,0,y)
View all 21 lines...
0
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 72 — 6y
0
@sahar1213 why dont you declare the variables at the beginning of the script and just use the changed event listener? aazkao 787 — 6y
0
@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. sahar1213 72 — 6y
0
Can somebody give a good awnser please? sahar1213 72 — 6y
Ad

Answer this question