Cash DevProduct not working

Hello.

So in my tycoon, I made a shop where people can purchase cash (e.g. 100 in-game cash for 10 R$)

The script is supposed to add 100 cash to the player if the purchase is successful. However after test purchase was done, no cash was added to my stats.

The script seems fine, so what could be an issue here?
It also doesn’t pop up any errors on the console.

4 Likes

You should post the script here using three ` at the top and bottom so we can help you. No one can help you without any information to why this issue is occurring.

-- If you do not know how to use this or the GUI, check out my YouTube video tutorial on this. 
-- Replace the 0's with your ID's 
-- Delete what you dont use, besides the first part of the function


game.StarterGui.ResetPlayerGuiOnSpawn = false
old_fog = game.Lighting.FogStart
local MarketplaceService = game:GetService("MarketplaceService")

function getPlayerFromId(id)
	for i,v in pairs(game.Players:GetChildren()) do
		if v.userId == id then
			return v
		end
	end
	return nil
end
 
MarketplaceService.ProcessReceipt = function(receiptInfo)
	local productId = receiptInfo.ProductId
	local playerId = receiptInfo.PlayerId
	local player = getPlayerFromId(playerId)
	local productName 
	
	-- Down below is an example of a Cash boost
	
	-------------------------------------------------------------------
	
	if productId == 736318162 then
		local cashmoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
			if cashmoney then
				cashmoney.Value = cashmoney.Value + 100
			       end
	elseif productId ==0  then
		local cashmoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
			if cashmoney then
				cashmoney.Value = cashmoney.Value + 10000
			end
	elseif productId == 0 then
		local cashmoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
			if cashmoney then
				cashmoney.Value = cashmoney.Value + 20000
			end
	elseif productId == 0 then
		local cashmoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
			if cashmoney then
				cashmoney.Value = cashmoney.Value + 30000
			end	
    elseif productId == 0 then
		local cashmoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
			if cashmoney then
				cashmoney.Value = cashmoney.Value + 40000
			end
	elseif productId == 0 then
		local cashmoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
			if cashmoney then
				cashmoney.Value = cashmoney.Value + 99999999999
			end
         end
	     return Enum.ProductPurchaseDecision.PurchaseGranted		
end
1 Like

The script adove handles how is the cash given to players.

I see at the top it says If you do not know how to use this or the GUI..

Is this a local script or a serverscript?

It is a serverscript.
And this is not mine, it was made by somebody else so I decided to use it for my shop.

I know how to use it, this script is the one who detects if Product is successfully purchased and then it grants money, another script is the one who prompts purchase which works fine.

However the thing is that it wouldn’t give cash even if purchase was successful.

Add some prints within the if statements and check if the script is even processing the purchase. Also, you can only use this function in one script otherwise it will have unexpected behaviour.

Are you sure that you have a PlayerMoney Folder inside your ServerStorage?
Also you didn’t replace the IDs for the 30000,40000 and 99999999999 Boosts. If you don’t Need them, you can remove them.

Nope, I do not have a PlayerMoney folder inside the ServerStorage. The KOs, WOs, and Cash are automatically added to leaderboard by tycoon script after server starts

And about IDs, the products are not yet ready, that’s why IDs aren’t replaced at the moment.

I’ve just tried to add another script from my obby which does the same thing - adds +1 to anything on leaderboar after purchase.

And this time again, purchase successful but no cash added. This script does both succesfully in obby tho.

That’s your issue.
Replace

local cashmoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name) if cashmoney then

with

local cashmoney = plr:FindFirstChild("leaderstats"):FindFirstChild("Cash") if cashmoney then

Alright, I’ll give it a try, hopefully it’ll work. :slight_smile:

1 Like

Just going to note you need to use FindFirstChild otherwise if leaderstats or cash doesn’t exist it will error

Use the search scripts function (Ctrl + Shift + F) and search for ProcessReceipt. If several come up, that’s your issue. ProcessReceipt can only be defined once and only the most recent assignment takes effect.

That’s the best I can assume if you aren’t receiving any errors or there doesn’t seem to be a problem with your code. That or the FindFirstChild statements are failing. You said the needed folder gets added by the tycoons so that can’t be the problem.

By the way, the getPlayerFromId function is unnecessary. There’s already a built-in function for that, Players.GetPlayerByUserId.

Edited, but why shouldn’t it exist?

Using plr["leaderstats"]["Cash"] is practically the same as doing plr.leaderstats.Cash, FindFirstChild returns nil or the child. That’s why the if cashmoney exists to check whether it’s nil or not.

You can read more on it here: https://developer.roblox.com/en-us/api-reference/function/Instance/FindFirstChild

– Didn’t read properly, I was assuming your code was looking for those children and then checking if it existed which wouldn’t exactly work as you’d assume.

You are Right, I actually knew.
I just was too lazy to remove the if clause/just didn’t think About it.

You should handle saving playermoney and dev products via datastores. All you gotta do is make the script wait for a purchase. Change the datastore value and save it. Whenever you need a script to use money, use the datastore value. You could also use leaderstats as values.

No you should not. You should handle data saving via data saving and purchase processing through purchase processing. DataStores are not a replacement for ProcessReceipt which is required to process developer products and receive earnings from them. Assuming this is a standard tycoon too, it doesn’t save on leave.

Doing it like this is a waste of DataStore budget, reinventing of the wheel and improper API use.

I guess you didn’t read that properly. You’d obviously want to keep the Buying and Processing in a different script. But keep the money value in the datastore. I was thinking he’d make a tycoon like restaurant tycoon. If its not a saving tycoon then all he has to do is when its purchased. Instead of changing a datastore value, all he has to do is change the leaderstat value.