ServerScriptService.Script:17: attempt to index nil with 'Gui'

When a player purchases a dev product this script is supposed to enable a Gui for all players, wait 60 seconds and disable it but I get this “ServerScriptService.Script:17: attempt to index nil with ‘Gui’”

Script:

local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local productId = 000000000
	
	local function processReceipt(receiptInfo)
		
		local player = players:GetPlayerByUserId(receiptInfo.PlayerId)
		if not player then
		
		
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
		
	if player then
			players:GetPlayers().PlayerGui.Gui.Enabled = true
			wait(60)
			players:GetPlayers().PlayerGui.Gui.Enabled = false
		
		
		
	end
		
		
	end


	MarketplaceService.ProcessReceipt = processReceipt
1 Like
  • Fire a RemoteEvent to the client from the server, when you want the gui to popup / go away.
  • Also include a value (true/false) in that remote, that you’re firing to the client.
  • On the client, make a function that listens to this RemoteEvent, and make it popup the GUI if your value sent is true, or go away if your value sent is false

Oh! or you could just remove it from the client after 60 secs in the client script, no need to fire a remote twice.

Here is the basics:

--SERVER
local TestRemote = ReplicatedStorage:WaitForChild("TestRemote")


-- Inside your processReceipt function
TestRemote:FireAllClients()


--CLIENT
local TestRemote = ReplicatedStorage:WaitForChild("TestRemote")
local Gui = script.Parent

TestRemote.OnClientEvent:Connect(function()
	-- The reason for that I check if Gui is already enabled, is that there's no reason to change the value, if it's already set to Enabled. And vice versa with when we disable the Gui.
	if not Gui.Enabled then
		Gui.Enabled = true
	end
	wait(60)
	if Gui.Enabled then
		Gui.Enabled = false
	end
end)

Switch this :

players:GetPlayers().PlayerGui.Gui.Enabled = true
wait(60)
players:GetPlayers().PlayerGui.Gui.Enabled = false

with this:

player.PlayerGui.Gui.Enabled = true
wait(60)
player.PlayerGui.Gui.Enabled = false

Rather than accessing the purchaser, you seem to be trying to access a table containing all the currently playing players.

1 Like

OK so this works great in studio, but when I try it in game nothing happens.

Did you buy the product while in the game also?

Yes, but it still does not work.

Sounds really strange, have you checked that the processReceipt goes through correctly, when the player buys a product?

I found out it was being caused by a donation board in my game which uses dev products, after removing the donation board everything works now. Thanks!

Try changing

players:GetPlayers().PlayerGui.Gui.Enabled = true
wait(60)
players:GetPlayers().PlayerGui.Gui.Enabled = false

to

for i, player in pairs(players:GetPlayers()) do
   player.PlayerGui.Gui.Enabled = true
   task.wait(60)
   player.PlayerGui.Gui.Enabled = false
end

I’m glad to hear that. You could as Deca have posted also change the gui.enabled directly from the server, but I wouldn’t personally go with that approach myself. With my approach you have the posibility to even tween the gui away.