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

Only 1 out of 9 when you buy currency in my game works, why?

It is supposed to work like if you press a button and pay the robux you get the currency, in this case "Bits". For some reason only the first one works "250 Bits". The buttons work but when you have pressed the buy button you get no bits. I can not even see that it printed in the output. Only the "Two_Fifty" one, that's the only that works. Why?

local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local TouchBuy = game.Workspace.Lobby.Interactions.PickGame
local PreviousPurchases = DataStoreService:GetDataStore("PreviousPurchases")
local gamefolder = game.ReplicatedStorage.Game
local runningFunction = gamefolder.RunningFunction

local Two_Fifty = 1147088183           -- 250
local Eight_Fifty = 1147088184         -- 850
local One_Five_Hundred = 1147088185    -- 1,500
local Two_Five_Hundred = 1147088188    -- 2,500 
local Eight_Five_Hundred = 1150501490  -- 8,500
local Fifteen_Tousand = 1150501488     -- 15,000
local Twenty_Five_Tousand = 1150501489 -- 25,000
local Eighty_Tousand = 1150501491      -- 80,000
local One_Fifty_Tousand = 1150501492   -- 150,000

MarketplaceService.ProcessReceipt = function(receipt)

	local ID = receipt.PlayerId.."-"..receipt.PurchaseId

	local success = nil

	pcall(function()
		success = PreviousPurchases:GetAsync(ID)
	end)

	if success then
		--Purchase has already been done
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

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

	if not player then
		--Left or disconnected
		return Enum.ProductPurchaseDecision.NotProcessedYet
	else
		
		if receipt.ProductId == Two_Fifty then
			print(player.Name.." bought 250 Bits!")
			local ls = player:FindFirstChild("leaderstats")	
			ls.Bits.Value = ls.Bits.Value +250
			
			if receipt.ProductId == Eight_Fifty then
				print(player.Name.." bought 850 Bits!")
				local ls = player:FindFirstChild("leaderstats")	
				ls.Bits.Value = ls.Bits.Value +850
			end
			
			if receipt.ProductId == One_Five_Hundred then
				print(player.Name.." bought 1,500 Bits!")
				local ls = player:FindFirstChild("leaderstats")	
				ls.Bits.Value = ls.Bits.Value +1500
			end
			
			if receipt.ProductId == Two_Five_Hundred then
				print(player.Name.." bought 2,500 Bits!")
				local ls = player:FindFirstChild("leaderstats")	
				ls.Bits.Value = ls.Bits.Value +2500
			end
			
			if receipt.ProductId == Eight_Five_Hundred then
				print(player.Name.." bought 8,500 Bits!")
				local ls = player:FindFirstChild("leaderstats")	
				ls.Bits.Value = ls.Bits.Value +8500
			end
			
			if receipt.ProductId == Fifteen_Tousand then
				print(player.Name.." bought 15,000 Bits!")
				local ls = player:FindFirstChild("leaderstats")	
				ls.Bits.Value = ls.Bits.Value +15000
			end
			
			if receipt.ProductId == Twenty_Five_Tousand then
				print(player.Name.." bought 25,000 Bits!")
				local ls = player:FindFirstChild("leaderstats")	
				ls.Bits.Value = ls.Bits.Value +25000
			end
			
			if receipt.ProductId == Eighty_Tousand then
				print(player.Name.." bought 80,000 Bits!")
				local ls = player:FindFirstChild("leaderstats")	
				ls.Bits.Value = ls.Bits.Value +80000
			end
			
			if receipt.ProductId == One_Fifty_Tousand then
				print(player.Name.." bought 150,000 Bits!")
				local ls = player:FindFirstChild("leaderstats")	
				ls.Bits.Value = ls.Bits.Value +150000
			end

			
		end

		pcall(function()
			PreviousPurchases:SetAsync(ID,true)
		end)
		return Enum.ProductPurchaseDecision.PurchaseGranted


	end

end

Thanks for the help, if you need more information, just ask :) , Xsodar

3 comments

0

On line 44 there should be an end to close the first if (for 250 bits).

0

This was the problem, I think, it works at the moment at least. Thanks for pointing that out :)

0

I urge you to accept my answer.

1 answer

Addendum to my comment.

-- ...
	if receipt.ProductId == Two_Fifty then
            print(player.Name.." bought 250 Bits!")
            local ls = player:FindFirstChild("leaderstats") 
            ls.Bits.Value = ls.Bits.Value +250
	end -- there should be an end here!
        
	if receipt.ProductId == Eight_Fifty then
            print(player.Name.." bought 850 Bits!")
            local ls = player:FindFirstChild("leaderstats") 
            ls.Bits.Value = ls.Bits.Value +850
	end

	if receipt.ProductId == One_Five_Hundred then
           print(player.Name.." bought 1,500 Bits!")
           local ls = player:FindFirstChild("leaderstats") 
           ls.Bits.Value = ls.Bits.Value +1500
	end
-- ...

Also you should use elseif instead of so many ifs, like so:

But I'm not familiar with the marketplace so maybe it won't work like this.

-- ...
	if receipt.ProductId == Two_Fifty then
            print(player.Name.." bought 250 Bits!")
            local ls = player:FindFirstChild("leaderstats") 
            ls.Bits.Value = ls.Bits.Value +250
       elseif receipt.ProductId == Eight_Fifty then
            print(player.Name.." bought 850 Bits!")
            local ls = player:FindFirstChild("leaderstats") 
            ls.Bits.Value = ls.Bits.Value +850
	elseif
	   receipt.ProductId == One_Five_Hundred then
           print(player.Name.." bought 1,500 Bits!")
           local ls = player:FindFirstChild("leaderstats") 
           ls.Bits.Value = ls.Bits.Value +1500
	end
-- ...

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