Can’t read variable from Bindable Event

  1. What do you want to achieve?: Read a variable using a Bindable Event

  2. What is the issue? The variable always returns nil, even though it is changed and the change is registered

  3. What solutions have you tried so far? I googled it, didn’t find any answers

local PickUpBall = {}
PickUpBall.__index = PickUpBall

local CS = game:GetService("CollectionService")

local BallInHand = nil

function PickUpBall:InputEnded()
    if BallInHand then return end
    
    local Character = PickUpBall.Owner.Character
    if not Character then return end
    
    local Ball = PickUpBall.Shared.FindBall()
    if not Ball then return end
    
    Ball.CanCollide = false
    Ball.CFrame = Character["Right Arm"].CFrame + Vector3.new(0,-1.5,0)
    
    CS:RemoveTag(Ball, "Soccer")
    
    local WeldConstraint = Instance.new("WeldConstraint", Ball)
    WeldConstraint.Name = "HoldConstraint"
    WeldConstraint.Part0 = Character["Right Arm"]
    WeldConstraint.Part1 = Ball
    
    PickUpBall.Events.PlayAnimation:Fire(11872244344, 0.5)
    
    BallInHand = Ball

        --a print(BallInHand) returns Ball
end

function PickUpBall:Cancel()
    if not BallInHand then return end
    
    CS:AddTag(BallInHand, "Soccer")
    if BallInHand:FindFirstChild("HoldConstraint") then
        BallInHand.HoldConstraint:Destroy()
    end
    
    BallInHand.CanCollide = true
    BallInHand = nil
end

function PickUpBall:Start()
    PickUpBall.Events.DropBall.Event:Connect(function()
        PickUpBall:Cancel()
    end)
    
    PickUpBall.Events.GetBallInHand.Event:Connect(function()
        return BallInHand -- Problematic line, it always returns nil
    end)
end

return PickUpBall

The only possible explinations I can think of is it’s returning nil before PickUpBall:InputEnded() is fired or after PickUpBall:Cancel is fired, I would suggest print debugging to pinpoint the issue

Already tried it, InputEnded is fired before the event, printing the BallInHand value returns the ball object, cancel is never fired, and even when it is, the first if statement blocks the rest of the function - also, the problem was occurring when the value wasn’t even being set to nil in the Cancel function

Also, trying to make the variable global, or adding it to the module table doesn’t change anything - i just changed the ball detection for now, but i’ll take any hints to why this problem occurred

You need to use a Bindable Function instead of a Bindable Event, since the former will yield for a returned value.

“When a script fires a BindableEvent it does not yield for a return. The script continues executing as the event is handled by the subscribed script asynchronously.”

You can read more about Bindable Functions here.

1 Like

That makes sense, i’ll still try it and see if it makes any difference

changing it to a bindable function fixed my problems, thanks

No problem! Be sure to mark the solution as correct for others. :wink:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.