Problem With Leaderstats

You have improper capitalization in the localscript for :GetService in line 2, but that should just make the script not work so are you sure that you don’t have another copy of the localscript you forgot to remove?

1 Like

okay i think there might have been a dupe of the script so ive fixed the spelling mistake and removed the other script. now however it prompts the purchase but does not add the cash. does the regular script for checking the remote event need to be anywhere specific?

1 Like

Sorry if i confused you i’m a little rusty i haven’t been in studio in a solid 8 months, have the server handle everything. Just use the localscript to detect when the button is clicked.

1 Like

im not sure how to do that using another script. I only know how to check if it has been purchased using the same script do u know how i can do that in a different script?

1 Like

Move everything you have besides what prompts the purchase to the server script and make it where the localscript just fires the remote event when the button is clicked and sends over the player, coins, and the id for the gamepass/dev product and in the server script you just do everything you did in the localscript (sorry for not having any scripts, i’m on phone so it’s a little hard)

maybe this will help because i’m not good at explaining:

1 Like

Servers can only see changes made by client through remotes.

But what you are doing now is handling purchase through client which should not be the case, I didn’t even know you can call processreceipt from a localscript. You should only handle purchase from the server.

Also clearly stated in the documentation.

1 Like

When i try handling the purchase in a server script i get this error:

MarketplaceService:PromptProductPurchase() player should be of type Player

1 Like

Promptproductpurchase can be done on the client but the handling of the purchases has to be made on the server.

2 Likes

Yeah thats the issue i said about earlier im not sure how to handle the purchase using a different script to where it is prompted from

1 Like

move the ProcessReceipt function to a server script

1 Like

I’m on mobile right now so I can’t help much but it shouldn’t be too complicated, earlier you made a script OnServerEvent which changes the leaderstat value, use that same principle but through ProcessReceipt on a server script.

1 Like

Its telling me i can only FireServer in a local script and thats part of the receipt that would need to be in a serverscript

1 Like

You dont need that part anymore, from the serverscript set the value of the leaderstats.

You only use fireserver if you are trying to send value from the client. But right now you are handling everything on the server so you can remove that portion and just set the leaderstats value.

1 Like

You can do something like this

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

local Products = {
	[3238402896] = 1000; --Key is your DevProduct ID and the value is how much coins
		
} -- Your DevProducts ID

function handlePurchase(Player, ProductId)
	if Products[ProductId] then
		local leaderstats = Player:FindFirstChild("leaderstats")
		
		if leaderstats then
			local Coins = leaderstats:FindFirstChild("Coins")
			if Coins then
				Coins.Value += Products[ProductId]
				return Enum.ProductPurchaseDecision.PurchaseGranted
			end
		end
	end
	
	return Enum.ProductPurchaseDecision.NotProcessedYet
end

MPS.ProcessReceipt = function(receiptinfo)
	local Player = Players:GetPlayerByUserId(receiptinfo.PlayerId)
	if not Player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	return handlePurchase(Player, receiptinfo.ProductId)
end

I’m on mobile so there might be error

1 Like

Yeah theres no error in that piece of code

2 Likes

The only thing i would recommend is if he is going to use more products that have different abilities he uses a product id as a key in the dictionary and its value is a function to perform its ability in the handle purchase function.

1 Like