BindableEvent parameter keeps returning nil

startTD.Event:Connect(function(path, player)
    gemsHandler.Setup(player, tycoon, path, 0) -- number is starting balance
    print("setup completed")
    updateGems:Fire(30)
    
    while task.wait(waveInterval) do
        local pathFolder 

        enemyHanlder.spawnEnemy("Peasant", 3, path, tycoon, player)
        pathFolder = tycoon.NPCs:FindFirstChild(path)

        repeat task.wait(1) until #pathFolder:GetChildren() == 0
    end
end)
function gems:UpdateBalance(amount)
    print(amount)
    if amount then
        if gems.CurrentBalance - amount <= 0 then
            gems.CurrentBalance = 0
        else 
            gems.CurrentBalance -= amount -- removing coins if NPCs make it through
        end
    end

    local gemsUI = gems.GemsCollector:WaitForChild("BillboardUI_Part", 10).GemAmountUI
    local gemsText = gemsUI:WaitForChild("MainFrame").GemLabel
    print(gems.CurrentBalance)

    gemsText.Text = "Gems to Collect: ".. gems.CurrentBalance
end

updateGems.Event:Connect(gems.UpdateBalance)

I am using a bindable event here but the 30 im passing through to the function in the second lot of code keeps printing as nil on line 30, im stuck can anyone help me here, I would appreciate it a lot, thanks

The script that contains this function: function gems:UpdateBalance(amount)

Its a Module Script?
If thats a module, theres no need you use a BindableEvent to send that data, you could just call the function inside that module from the first script directly

The reason why you are getting nil its because the function its called like this:
function gems:UpdateBalance(amount)

and you are calling this from the fired event:
updateGems.Event:Connect(gems.UpdateBalance)

Just change the name of the function to function gems.UpdateBalance(amount)

But I insist, its better idea to use the module as it should, thats what makes modules very flexible and helpful, you can just call the function in that module from the first script without firing any BindableEvent

The first script:

local gems = require(game.ServerScriptService:WaitForChild("gems"))
gems.UpdateBalance(30)

The module:

local gems = {}

function gems.UpdateBalance(amount)
	warn(amount)
end

return gems

i made a typo srry im slow thx for the help tho

Cool, you solved it:+1: :upside_down_face: 30kchaaars

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