How to communicate Local Script in StarterGui to ModuleSript in Replicated Storage?

Soo a little while back @ForeverHD made this awesome open-sourced Crate/Spin system that is pretty amazing. Latley, I have been playing with this crate system and decided to make it so that in order to spin the crate you must pay 50 Robux per spin. All was good until, I wasn’t able to verify the purchase and make the crate begin.

My Attempted Solutions

  • Use a remote event from the local script to server script, then to the module script.
    • Remote events don’t work on module scripts
  • _G global variables
    • This doesn’t work because global varaibles can only communicate to other scripts under their main parent (e.g. ServerStorage, ServerScriptService)

Some Scripts

Script for where the player clicks the “Spin” button and prompts to pay 50 Robux.
If you want to see the whole script, it’s in the link to the tutorial. This is a local script under StarterGui

--Select Spin
spin.MouseButton1Down:Connect(function()
	if spinDe then
		spinDe = false
		local originalSpinText = spin.TextLabel.Text
		
		-- Purchase is made here
   		local productId = 574458039
   		game:GetService("MarketplaceService"):PromptProductPurchase(game.Players.LocalPlayer, productId)
		 -- Checks to see if player has permission (Function sent from the module script)
		if not contents:PermissionToSpin(player) then
			spin.TextLabel.Text = contents.Messages.FailPermissionCheck
			wait(1)
		else
			spin.TextLabel.Text = contents.Messages.Loading
			local spinDetails, errorMessage = rfunction:InvokeServer()
			if not spinDetails then
				if not errorMessage then
					errorMessage = contents.Messages.ServerError
				end
				spin.TextLabel.Text = errorMessage
				wait(1)
			else
				SpinFunction(spinDetails)
			end
		end
		spin.TextLabel.Text = originalSpinText
		spinDe = true
	end
end)

Here is the Module script with the value that needs to be set to “true” upon purchase.

-- << Permission to Spin >>

function contents:PermissionToSpin(player)

-- Here is the purchase was made it would be set to "return true"

end

Ultimate Goal

  1. Player Clicks “Spin”
  2. Prompts player to spend 50 robux
  3. If spends the 50 robux, the module script would return true.
  4. The crate appears and player claims prize
  5. Redo the proccess
1 Like

You should invoke the spin after the callback of MarketplaceService.ProcessReceipt. This callback fires when a user has tried to buy something and you need to confirm it on the server. For more info you can read the article about it here:

https://developer.roblox.com/api-reference/callback/MarketplaceService/ProcessReceipt

Bonus: The code in the article provides a nice way to structure functions so you can understand what it does easier.

Edit: For some reason the link went missing so maybe I fixed it?

2 Likes

Not sure what you mean by this? Remotes work with and on ModuleScripts. What was your original implementation’s workflow?

_G (and shared) (global tables) can be used by any script of the same environment, server or client. The scripts do not have to be under the same parent. _G in LocalScripts point to the same table, as does _G in server scripts. _G in a ModuleScript is dependent on the script that required it.

The original interpretation was after player purchased the developer product it would fire a signal to the a Remote Event inside replicated storage, then it would go to a script in server script service, then finally into the module script inside replicated storage. The problem, is that the server script can only go to the local script.