Buy Currency System Not Working In Roblox Game - But Working In Roblox Studio

Hey, Iโ€™m getting this error:


Although it is a valid member
image

Handler

local mps = game:GetService("MarketplaceService")
local coinsProductIDs = 
	{
		[1270183510] = 500,
		[1270183548] = 1000,
		[1270183588] = 2500,
		[1270183665] = 10000,
		[1270184043] = 25000,
		[1270183750] = 100000,
	}

mps.ProcessReceipt = function(purchaseInfo)


	local plrPurchased = game.Players:GetPlayerByUserId(purchaseInfo.PlayerId)

	if not plrPurchased then

		return Enum.ProductPurchaseDecision.NotProcessedYet
	end


	for productID, coinsGiven in pairs(coinsProductIDs) do

		if purchaseInfo.ProductId == productID then


			plrPurchased.leaderstats['๐Ÿ•™ Time'].Value = plrPurchased.leaderstats['๐Ÿ•™ Time'].Value + coinsGiven

			return Enum.ProductPurchaseDecision.PurchaseGranted
		end
	end
end

The script is used for a system to purchase leaderstat coins via a gui button, it works in studio test but not in the game itself which is really old, does anyone know?

The other script is (local)

local mps = game:GetService("MarketplaceService")

local plr = game.Players.LocalPlayer


local coinsProductIDs = 
	{
		1270183510,
		1270183548,
		1270183588,
		1270183665,
		1270184043,
		1270183750,
	}


local purchase500CoinsBtn = script.Parent.t500.TextButton
local purchase1000CoinsBtn = script.Parent.t1000.TextButton
local purchase2500CoinsBtn = script.Parent.t2500.TextButton
local purchase10000CoinsBtn = script.Parent["t10,000"].TextButton
local purchase25000CoinsBtn = script.Parent["t25,000"].TextButton
local purchase100000CoinsBtn = script.Parent["t100,000"].TextButton


local function purchaseCoinsFunction(key)


	local idToPurchase = coinsProductIDs[key]

	mps:PromptProductPurchase(plr, idToPurchase)
end


purchase500CoinsBtn.MouseButton1Click:Connect(function()
	purchaseCoinsFunction(1)
end)

purchase1000CoinsBtn.MouseButton1Click:Connect(function()
	purchaseCoinsFunction(2)
end)

purchase2500CoinsBtn.MouseButton1Click:Connect(function()
	purchaseCoinsFunction(3)
end)

purchase10000CoinsBtn.MouseButton1Click:Connect(function()
	purchaseCoinsFunction(4)
end)

purchase25000CoinsBtn.MouseButton1Click:Connect(function()
	purchaseCoinsFunction(5)
end)

purchase100000CoinsBtn.MouseButton1Click:Connect(function()
	purchaseCoinsFunction(6)
end)
1 Like

Iโ€™m not sure why you would be running into this issue, unless the leaderstats was created on the client (in a LocalScript) or the product was purchased before the leaderstats folder was added.

Why donโ€™t you try :WaitForChild() on the leaderstats? It could be possible that the leaderstats didnโ€™t load fast enough.

New Handler
local mps = game:GetService("MarketplaceService")
local coinsProductIDs = 
	{
		[1270183510] = 500,
		[1270183548] = 1000,
		[1270183588] = 2500,
		[1270183665] = 10000,
		[1270184043] = 25000,
		[1270183750] = 100000,
	}

mps.ProcessReceipt = function(purchaseInfo)


	local plrPurchased = game.Players:GetPlayerByUserId(purchaseInfo.PlayerId)

	if not plrPurchased then

		return Enum.ProductPurchaseDecision.NotProcessedYet
	end


	for productID, coinsGiven in pairs(coinsProductIDs) do

		if purchaseInfo.ProductId == productID then

			local plrLeaderstats = plrPurchased:WaitForChild("leaderstats")
			plrLeaderstats['๐Ÿ•™ Time'].Value = plrLeaderstats['๐Ÿ•™ Time'].Value + coinsGiven

			return Enum.ProductPurchaseDecision.PurchaseGranted
		end
	end
end

If you get an infinite yield warning, then it must not be found on the server. If thatโ€™s the case, then try clicking this button.
image
It should say โ€œCurrent: Serverโ€ after clicking, and the game tab should also be outlined in green instead of blue (shown in your image). Navigate to the leaderstats in the player. If itโ€™s not there, then it was probably created on the client.

3 Likes

just tested this, and it worked perfectly in roblox studio, i went into a real roblox game, and when purchased didnt get awarded, neither no errors anymore

May I see your script which creates the leaderstats?

1 Like
local DataStoreService = game:GetService("DataStoreService")
local CurrencyStore = DataStoreService:GetDataStore("CurrencyStore")
local Players = game:GetService("Players")
local GamepassID = 26754497


-- leaderstats/stats/๐Ÿ•™ Time

game.Players.PlayerAdded:Connect(function(player)
	local humanoid = workspace:WaitForChild(player.name):WaitForChild("Humanoid")
	
	
	

	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	

	local T = Instance.new("IntValue")
	T.Name = "๐Ÿ•™ Time"
	T.Parent = leaderstats
	task.spawn(function() 
		while task.wait(1) do 
			
		
		local ownsGamepass = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, GamepassID)
        if ownsGamepass then
          T.Value+= 1 * 2
			else
				T.Value += 1
			end
			
			
			
		end 
	end)

	local data
	local suc, err = pcall(function()
		data = CurrencyStore:GetAsync(player.UserId)
	end)

	if (data) then
		T.Value = data
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local data = player.leaderstats["๐Ÿ•™ Time"].Value
	

	local suc, err = pcall(function()
		data = CurrencyStore:SetAsync(player.UserId, data)
		
	end)

	if not suc then 
		warn(err)
		print(err)
	end
end)

does anyone else know then please?

I recall having the same issue with leaderstats forever ago, Iโ€™ll see if I can find out what I did to fix it
Why is this line here tho?

1 Like

i believe i had forgotten to remove it, but i think it was for inserting bool values into humanoids
but its not that cause of the issue as i had just tested without it
if you can figure out what you did to fix it, that would help out as i cant figure this out, it just kinda seems more roblox bug related since the roblox studio is meant to replicate what a actual roblox client experiences

Late response, Iโ€™m just super terrible at dev products
can you try doing this and see if any errors pop up?

local stats = plrPurchased:WaitForChild("leaderstats",10)
stats['๐Ÿ•™ Time'].Value += coinsGiven
1 Like

Iโ€™ll try this after school, thanks

What this does is, basically yields (pauses) the script until it finds the leaderstats or the 10 seconds is over. If the leaderstats isnโ€™t found within the 10 seconds, you will get the infinite yield warning, because the script cannot find the leaderstats.

1 Like

Do you have another script somewhere assigning a function to ProcessReceipt? (Ctrl+Shift+F ProcessReceipt)

1 Like

I have changed your ProcessReceipt script around a bit, it should actually reward the player who purchases nowโ€ฆ

ProcessReceipt
local mps = game:GetService("MarketplaceService")
local coinsProductIDs = 
	{
		[1270183510] = 500,
		[1270183548] = 1000,
		[1270183588] = 2500,
		[1270183665] = 10000,
		[1270184043] = 25000,
		[1270183750] = 100000,
	}

local coinsProductsHandlers = {}

for productID, coinsGiven in pairs(coinsProductIDs) do
	if coinsProductsHandlers[productID] == nil then
		coinsProductsHandlers[productID] = function(purchaseInfo, plrPurchased)
			local leaderstats = plrPurchased:WaitForChild("leaderstats")
			leaderstats['๐Ÿ•™ Time'].Value += coinsGiven
			return true
		end
	end
end

mps.ProcessReceipt = function(purchaseInfo)
	local plrPurchased = game.Players:GetPlayerByUserId(purchaseInfo.PlayerId)
	if not plrPurchased then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
    
	if coinsProductsHandlers[purchaseInfo.ProductId] then
		local success, isPurchased = pcall(coinsProductsHandlers[purchaseInfo.ProductId], purchaseInfo, plrPurchased)
		if not success or not isPurchased then
			return Enum.ProductPurchaseDecision.NotProcessedYet
		else
			return Enum.ProductPurchaseDecision.PurchaseGranted
		end
	end
	return Enum.ProductPurchaseDecision.NotProcessedYet
end
1 Like

doesnt work but ty for trying
:DDDDDDDDDDDDDD

tested and there was no infinite yield warning, nor did the script reward player

OMG! I GOT IT WORKING!

  • I scrapped this script, and used a promptproductpurchase + promptproductpurchasedfinished (this has a little disadvantage since the player only gets the currency after pressing the ok button after purchasing, but it is fine.

Hereโ€™s what I did

  • Please note you need to make a remote-event for your individual purchasable currencies, it canโ€™t just be one

A LOCAL SCRIPT under the gui button:

local purchase500CoinsBtn = script.Parent.t500.TextButton
local purchase1000CoinsBtn = script.Parent.t1000.TextButton
local purchase2500CoinsBtn = script.Parent.t2500.TextButton
local purchase10000CoinsBtn = script.Parent["t10,000"].TextButton
local purchase25000CoinsBtn = script.Parent["t25,000"].TextButton
local purchase100000CoinsBtn = script.Parent["t100,000"].TextButton

local replicate = game.ReplicatedStorage.currency

purchase500CoinsBtn.MouseButton1Click:Connect(function()
	replicate.t500:FireServer()
end)

purchase1000CoinsBtn.MouseButton1Click:Connect(function()
	replicate.t1000:FireServer()
end)

purchase10000CoinsBtn.MouseButton1Click:Connect(function()
	replicate.t10000:FireServer()
end)

purchase25000CoinsBtn.MouseButton1Click:Connect(function()
	replicate.t25000:FireServer()
end)

purchase2500CoinsBtn.MouseButton1Click:Connect(function()
	replicate.t2500:FireServer()
end)

purchase100000CoinsBtn.MouseButton1Click:Connect(function()
	replicate.t100000:FireServer()
end)

Next make a SERVER SCRIPT in ServerScriptService:

--
local t500 = 1270183510
local t500event = game.ReplicatedStorage.currency.t500
-- 
--
local t1000 = 1270183548
local t1000event = game.ReplicatedStorage.currency.t1000
-- 
--
local t2500 = 1270183588
local t2500event = game.ReplicatedStorage.currency.t2500
-- 
--
local t10000 = 1270183665
local t10000event = game.ReplicatedStorage.currency.t10000
-- 
--
local t25000 = 1270184043
local t25000event = game.ReplicatedStorage.currency.t25000
-- 
--
local t100000 = 1270183750
local t100000event = game.ReplicatedStorage.currency.t100000
-- 


local MS = game:GetService("MarketplaceService")
t500event.OnServerEvent:Connect(function(plr)
	MS:PromptProductPurchase(plr, t500)
end)

t2500event.OnServerEvent:Connect(function(plr)
	MS:PromptProductPurchase(plr, t2500)
end)

t10000event.OnServerEvent:Connect(function(plr)
	MS:PromptProductPurchase(plr, t10000)
end)


t25000event.OnServerEvent:Connect(function(plr)
	MS:PromptProductPurchase(plr, t25000)
end)


t100000event.OnServerEvent:Connect(function(plr)
	MS:PromptProductPurchase(plr, t100000)
end)


t1000event.OnServerEvent:Connect(function(plr)
	MS:PromptProductPurchase(plr, t1000)
end)



MS.PromptProductPurchaseFinished:Connect(function (plr, e_productId, isPurchased)
	if e_productId == t500 and isPurchased == true then
		print("Currency: 500+")
		local plrPurchased = game.Players:GetPlayerByUserId(plr)
		plrPurchased.leaderstats['๐Ÿ•™ Time'].Value = plrPurchased.leaderstats['๐Ÿ•™ Time'].Value + 500
		
	elseif e_productId == t1000 and isPurchased == true then
		print("Currency: 1,000+")
		local plrPurchased = game.Players:GetPlayerByUserId(plr)
		plrPurchased.leaderstats['๐Ÿ•™ Time'].Value = plrPurchased.leaderstats['๐Ÿ•™ Time'].Value + 1000
		
	elseif e_productId == t2500 and isPurchased == true then
		print("Currency: 2,500+")
		local plrPurchased = game.Players:GetPlayerByUserId(plr)
		plrPurchased.leaderstats['๐Ÿ•™ Time'].Value = plrPurchased.leaderstats['๐Ÿ•™ Time'].Value + 2500
		
	elseif e_productId == t10000 and isPurchased == true then
		print("Currency: 10,000+")
		local plrPurchased = game.Players:GetPlayerByUserId(plr)
		plrPurchased.leaderstats['๐Ÿ•™ Time'].Value = plrPurchased.leaderstats['๐Ÿ•™ Time'].Value + 10000
		
	elseif e_productId == t25000 and isPurchased == true then
		print("Currency: 25,000+")
		local plrPurchased = game.Players:GetPlayerByUserId(plr)
		plrPurchased.leaderstats['๐Ÿ•™ Time'].Value = plrPurchased.leaderstats['๐Ÿ•™ Time'].Value + 25000
		
	elseif e_productId == t100000 and isPurchased == true then
		print("Currency: 100,000+")
		local plrPurchased = game.Players:GetPlayerByUserId(plr)
		plrPurchased.leaderstats['๐Ÿ•™ Time'].Value = plrPurchased.leaderstats['๐Ÿ•™ Time'].Value + 100000
	end
end)

It now works in game, feel free to change it to adjust to yours.

1 Like