Spent the last 2 days making a coins system and it does not work (shocker)

image

code. the code does not work. i tried troubleshooting with output + printing but now theres no errors and it doesnt work as intended. it doesnt add the coins to your balance and ive checked the paths many times, but it still does not work.

if someone understands my issue please let me know asap i need sleep

1 Like

If you want to learn how to debug you need to learn how to use print statements. Print out the player’s name, print out something simple to see if each part of the script works. I believe one problem is that you are doing

local player = game.Players:GetPlayerByUserId(receiptInfo.UserId)

when you should be doing

local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)

Also please copy and paste your code rather than screenshotting it is just annoying to write out parts of people’s code.

1 Like

Instead of receiptInfo.UserId you should use receiptInfo.PlayerId, I tried cleaning your code to avoid bad scripting habits(like repetitive if statements, WaitForChild inside a yielding function, not using a pcall to avoid errors, handling situations etc.)

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

--format [coin amount] = product id
local products = {
	[100] = 1229114461,
	[1000] = 1229114462,
	[5000] = 1229114463
}

MarketplaceService.ProcessReceipt = function(receiptInfo)
	local Player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
	
	if not Player then 
		--if the player is not found, we ignore the request
		return Enum.ProductPurchaseDecision.NotProcessedYet 
	end
	
	--we loop through each product
	for amount, product in pairs(products) do 
		--we keep skipping products until we find the one matching
		if receiptInfo.ProductId ~= product then continue end
		
		--if the product matches the we wrap the coin change in a pcall to know if it fails
		local Success, Error = pcall(function()
			Player.leaderstats.Coins.Value += amount
		end)
		--if the coins do chance, then actually pay for the product
		if Success then 
			return Enum.ProductPurchaseDecision.PurchaseGranted 
		else 
			warn(Error)
			return Enum.ProductPurchaseDecision.NotProcessedYet 
		end
	end
	--in case the product id isn't found(the purchase is invalid) it just fails
	--you can remove the line below if you just want to ignore invalid products
	return Enum.ProductPurchaseDecision.NotProcessedYet
end
2 Likes

fixed that, doesnt work. and here it is

local mps = game:GetService("MarketplaceService")

local coins100 = 1229114461
local coins1000 = 1229114462
local coins5000 = 1229114463

local function processReceipt(receiptInfo)
	local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
	
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	if player then
		
		if receiptInfo.ProductId == coins100 then
			local ls = player:WaitForChild("leaderstats")
			ls.Coins.Value = ls.Coins.Value + 100
		end

		if receiptInfo.ProductId == coins1000 then
			local ls = player:WaitForChild("leaderstats")
			ls.Coins.Value = ls.Coins.Value + 1000
		end
		if receiptInfo.ProductId == coins5000 then
			local ls = player:WaitForChild("leaderstats")
			ls.Coins.Value = ls.Coins.Value + 5000
		end
	end
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

mps.ProcessReceipt = processReceipt
1 Like

tried that doesnt work. thanks for the help though

1 Like

are you using more than one process receipt? you can only call one at a time or else you’ll have bugs

1 Like

theres another but its not in the same script. it doesnt run at the same time as this one either

1 Like

Have you tried disabling the other script for testing purposes?

1 Like

tried it just now. does not work

1 Like

The script works perfectly fine when I play-test it, add a print("success") inside the pcall to see if the script is running at intended(also add another print on the top of the function to see if the event fires at all).

No output. sorry i was out for a minute, was getting a bite.

Did you try purchasing one of the products, that are defined in the table, while the other script was disabled, and no output(in my own script)?

local mps = game:GetService("MarketplaceService")

local coins100 = 1229114461
local coins1000 = 1229114462
local coins5000 = 1229114463

local function processReceipt(receiptInfo)
	local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)

	if player then
		local ls = player:WaitForChild("leaderstats")
		local coins = ls:WaitForChild("Coins")
		
		if receiptInfo.ProductId == coins100 then
			coins.Value += 100
		elseif receiptInfo.ProductId == coins1000 then
			coins.Value += 1000
		elseif receiptInfo.ProductId == coins5000 then
			coins.Value += 5000
		end
		
		return Enum.ProductPurchaseDecision.PurchaseGranted
	else
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
end

mps.ProcessReceipt = processReceipt

just did. does not work, thanks for the help though

thanks for the help but doesnt work once again. not sure what the deal is with this script

Can you show a screenshot in game with the leaderboard?

Roblox Studio - Gyazo

does not work even if i disable every script in the game except leaderstats and datastors and the shop script

Test.rbxl (28.5 KB)

Same script, I added the leaderstats & a simple Gui with a button which prompts a purchase for the developer product.

Update: disabled virtually every script that doesnt affect data, leaderstats and the shop and it works.

Figured out that scripts in workspace are the culprits because when i reenabled those the script does not work.

we have one donation board, is it possible that is interfering?