Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Waiting for return?

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?

1 answer

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().

4 comments

0

"the button can be used over and over. ". But wipe function closes window after pressing button so it can't be presed 'over and over'. Interesting idea about function. So you telling me to give to Module function as argument that will be executed after successful purchase? Nice idea, will try if later.

0

Also, I believe that due to the module returning the "buyFunction" function, the "Wipe" function will not be passed back, so the module will err when it calls it.

0

No, it won't error. It keeps the environment where it was defined.

0

@BlueTaslem found a way to fix. I wil return some variable at the end of function, but before wait until user make choice and set this var.

This discussion is preserved as it was. New answers, comments, and votes are closed.