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

Buy cash gui wont add cash when bought?

I have a buy cash gui but when i do a text purchase, it doesnt add 500 cash. here is the script.

local buyButton = script.Parent
local productId = 92041867 --change to dev product id
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"Cash" then
plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + 500
end
end

Here is my leaderboard script -- also sometimes instead of saying cash, it says value and I dont know why.

local playerLeaderstats = {}

game.Players.PlayerAdded:connect(function(player)

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

    local rank = Instance.new("StringValue", leaderstats)
    rank.Name = "Rank"
    rank.Value = player:GetRoleInGroup(3309505) 

    playerLeaderstats[player] = {}
    playerLeaderstats[player]["Cash"] = 100

    local money = Instance.new("IntValue", leaderstats)
    money.Name = "Cash"
    money.Value = playerLeaderstats[player]["Cash"]

    while wait(5) do 
        playerLeaderstats[player]["Cash"] = playerLeaderstats[player]["Cash"] + 1
        if player:FindFirstChild("leaderstats") ~= nil then
            player.leaderstats.Cash.Value = playerLeaderstats[player]["Cash"]
        end
    end

end)

2 comments

0

What game is this for? A tycoon? If it is a tycoon, are you using a tycoon kit?

0

No, its not a tycoon. Here I will put in my leaderboard so that people can understand because None of these work and I think its my leaderboard's problem

3 answers

Locally this works. In an actual server this won't work because you can't use ProcessReceipt through a localscript. You should be prompting the purchase through the client and processing it through the server.

This wiki article has a nice ProcessReceipt example.

This one is also helpful.

Oh, and this one.

Here's a Filtering Enabled compatible version of your code.

Script in ServerScriptService

local MarketplaceService = game:GetService("MarketplaceService")
local ProductID = 92041867

MarketplaceService.ProcessReceipt = function(info)
	
	-- All of this code is a slighly modified version of the code on the wiki.
	local player = game:GetService("Players"):GetPlayerByUserId(info.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	if info.ProductId == ProductID then
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 500
	end	
	
	return Enum.ProductPurchaseDecision.PurchaseGranted	
	
end

LocalScript inside of your TextButton.

local MarketplaceService = game:GetService("MarketplaceService")
local ProductID = 92041867

script.Parent.MouseButton1Down:connect(function()
	MarketplaceService:PromptProductPurchase(game.Players.LocalPlayer, ProductID)
end)

Edit: From your leaderboard script, it looks like the issue is that you're setting the value of cash to a separate value every 5 seconds. This overrides the value in the leaderboard. After processing your receipt you should be updating your dictionary rather than the leaderboard.

Here's a new basic leaderboard script. This script is both creating and updating the leaderboard and processing receipts.

local MarketplaceService = game:GetService("MarketplaceService")
local ProductID = 92041867

local playerLeaderstats = {}

game.Players.PlayerAdded:connect(function(player)

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

    local rank = Instance.new("StringValue", leaderstats)
    rank.Name = "Rank"
    rank.Value = player:GetRoleInGroup(3309505) 

    playerLeaderstats[player] = {}
    playerLeaderstats[player]["Cash"] = 100

    local money = Instance.new("IntValue", leaderstats)
    money.Name = "Cash"
    money.Value = playerLeaderstats[player]["Cash"]

    while wait(5) do 
        playerLeaderstats[player]["Cash"] = playerLeaderstats[player]["Cash"] + 1
        if player:FindFirstChild("leaderstats") ~= nil then
            player.leaderstats.Cash.Value = playerLeaderstats[player]["Cash"]
        end
    end

end)

MarketplaceService.ProcessReceipt = function(info)
	
	-- All of this code is a slighly modified version of the code on the wiki.
	local player = game:GetService("Players"):GetPlayerByUserId(info.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	if info.ProductId == ProductID then
		playerLeaderstats[player]["Cash"] = playerLeaderstats[player]["Cash"] + 500
		if player:FindFirstChild("leaderstats") ~= nil then
            player.leaderstats.Cash.Value = playerLeaderstats[player]["Cash"]
        end
	end	
	
	return Enum.ProductPurchaseDecision.PurchaseGranted	
	
end

6 comments

0

You beat me to it, gg

0

By like 30 seconds. lol

0

Here I will put in my leaderboard so that people can understand because None of these work and I think its my leaderboard's problem

0

it does work, but when i do the test purchase and it adds 500, the cash on the leaderboard just goes back to 100

0

From my testing this works fine. Make sure you replaced your old script with this one. If you have both your old one and this one it may break it.

0

sometimes it still says value instead of cash

Hi sweet, It is hard right now to know the solution because we don't know what your game is about. Please try using this and see if it works:

--PUT THIS IN A SCRIPT IN THE ServerScriptService

local MPS = game:GetService'MarketplaceService'

MPS.ProcessReceipt = function(info)
	local plr = game:GetService'Players':GetPlayerByUserId(info.PlayerId)
	if plr then
		local prodid = info.ProductId
		if prodid =  92041867  then
			if plr:FindFirstChild'leaderstats' and plr.leaderstats:FindFirstChild'Cash' then
				plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + 500
				print('given')
			end
		end
	end
end

Now, put this code in a LocalScript in your Button


local buyButton = script.Parent
local productId = 92041867 --change to dev product id
local mps = game:GetService"MarketplaceService"

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

Please test and if it works please click Accept Answer. If it doesn't, just tell me and I'll be happy to help :) Also, try to give me what the Developer Console (Local & Server) says if it doesnt work.

It requires parenthesis

local mps = game:GetService("MarketplaceService")

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