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

Why my dev product doesn't work?

So I tried to make one and it looks like it should work, but I ran test, spent 5 robux and I didin't get thing that I should get when I buy that product. Here is the script :

local buyButton = script.Parent
local productId = 33818042
local mps = game:GetService("MarketplaceService")

function getPlayerById(id)
for i,v in pairs(game.Players:GetPlayers()) do
if v.userId == id then
return v
end
end
end

buyButton.MouseButton1Click:connect(function()
mps:PromptProductPurchase(game.Players.LocalPlayer, productId)
end)

mps.ProcessReceipt = function(info)
local plr = getPlayerById(info.PlayerId)
if plr and plr:FindFirstChild"leaderstats" and plr.leaderstats:FindFirstChild"Gems" then
plr.leaderstats.Gems.Value = plr.leaderstats.Gems.Value + 100
end
end

It seems like it should work but I didin't get my 100 gems. Help please.

2 answers

✓ Accepted answer

Not sure if this will work but it's worth a try!

-- Try putting this script into StarterPack

local MarketplaceService = game:GetService("MarketplaceService")
local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
 
GEMSID = 33818042

MarketplaceService.ProcessReceipt = function(receiptInfo)
	local playerProductKey = "player_" .. receiptInfo.PlayerId .. "_product_" .. receiptInfo.ProductId
	local numberBought = ds:IncrementAsync(playerProductKey, 1)
	for i,v in pairs (game.Players:GetChildren()) do
		if v.userId == receiptInfo.PlayerId then
			if receiptInfo.ProductId == GEMSID then
			
			
				lds = v:FindFirstChild("leaderstats")
				if lds ~= nil then
					cs = lds:FindFirstChild("Gems") 
					if cs ~= nil then
						cs.Value = cs.Value + 100
					end
				end
			end
		end
	end
	return Enum.ProductPurchaseDecision.PurchaseGranted		
end

A few things: If you want to get a player from their userId, use this function.

Your if statement on Line 19 should be all one line. It looks really confusing otherwise.

Put parentheses around your arguments, otherwise it again looks confusing and isn't really the proper way to do things.

Now, to answer your question: It looks like to me you forgot to put a GetChildren() when you tried to index the players class. I don't see else anything wrong with your code, but I recommend you use the method provided by ROBLOX above, and if you still don't get your gems, it most likely is something with your leader board.

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