I have Script and ModuleScript. This is part of how Script looks:
owner = require(script.Owner) function process(player) if owner == nil then var = require(player.PlayerGui.buyGui.buyFrame.buyScript)(player, price, name, image) print(var) end end
And full ModuleScript:
function wipe() script.Parent.ProductPrice.Text = '%price%' script.Parent.houseImage.Image = 'rbxassetid://133293265' script.Parent.ProductName.Text = '%name%' script.Parent.Visible = false end local buyFunction = function(player, price, name, image) script.Parent.ProductPrice.Text = price script.Parent.houseImage.Image = image script.Parent.ProductName.Text = name local verify function verify() if player.leaderstats.Cash.Value >= price then player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - price wipe() return 'success' elseif player.leaderstats.Cash.Value < price then wipe() return 'no_money' end end local close function close() wipe() return 'aborted' end script.Parent.BuyButton.MouseButton1Click:connect(verify) script.Parent.CancelButton.MouseButton1Click:connect(close) script.Parent.Visible = true end return buyFunction
As you can see, ModuleScript returning status of purchase. But var in Script always is nil. It is because Script not waiting for completing actions in ModuleScript. So how to fix that? I think about BindableFunction for that, but interested is there a way to fix it?
buyFunction
doesn't return anything, so it makes sense that you get nil
.
There's no real reason why the Script needs the module to return anything. The module sets up a button -- the button can be used over and over. Getting the result of the first push just doesn't make sense.
Instead, it might make sense to give it a function telling it what to do when they make a purchase.
You can use local function verify()
instead of local verify function verify()
.