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 5 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!

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)

1 answer

Log in to vote
1
Answered by
Aimarekin 345 Moderation Voter
5 years ago
Edited 5 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.

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
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 — 5y
0
@sahar1213 why dont you declare the variables at the beginning of the script and just use the changed event listener? aazkao 787 — 5y
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 — 5y
0
Can somebody give a good awnser please? sahar1213 72 — 5y
Ad

Answer this question