How is this possible?

So, I recently asked a question about the purchase leaderstats, Anyways it’s basically a leaderstats to when someone buys a outfit it adds 1 point to his purchased…The script works like this:

local MPS = game:GetService("MarketplaceService")
local shirtId = 8695108536
game:GetService("MarketplaceService").PromptPurchaseFinished:Connect(function(player, shirtId, isPurchased)
	if isPurchased then
		print(player.Name .. " bought an item with AssetID: " .. shirtId)
		local leaderstats = player.leaderstats
		local purchaseStat = leaderstats and leaderstats:FindFirstChild("Purchased")
		if purchaseStat then
			purchaseStat.Value += 1
		end
	else
		return
	end
end)

But apparently it works on any shirts…? I am really confused about this one since I only added 1 shirt’s id. If someone knows why it does this or how to explain what just happened I would highly appreciate it =)

It’s because you never check if the id of the item that he bought is the same as the one you want to check. You specified the shirtId in the second line, but then that same shirtId variable gets overwritten by the one that the function gets ( function(player, shirtId --THIS ...) ). You have to make the two variables different, for example, make the first one called “shirtId” and the second (inside the function) just simply “Id”, and then you can check if they are the same.

local MPS = game:GetService("MarketplaceService")
local shirtId = 8695108536
game:GetService("MarketplaceService").PromptPurchaseFinished:Connect(function(player, Id, isPurchased)
	if isPurchased and Id == shirtId then -- checks if they're the same
		print(player.Name .. " bought an item with AssetID: " .. shirtId)
		local leaderstats = player.leaderstats
		local purchaseStat = leaderstats and leaderstats:FindFirstChild("Purchased")
		if purchaseStat then
			purchaseStat.Value += 1
		end
	else
		return
	end
end)
5 Likes

Still really confusing…But will try to get the hang of it someday lol

To try to explain as well,

What they were trying to explain was that you had no condition that checks if the id of the shirt matches the id that causes an increment in your IntValue,

PromptPurchaseFinished, as I think you already know looking at your code, gives you 3 things to use

  • The player instance that did the purchase
  • The id of the asset that was bought (Meaning that it’s not just shirts that causes this event to run)
  • If the purchase was successful (false on errors and cacellations)

If you want to ensure the IntValue only increments if a specific shirt was bought, you just compare the ids as they did with the Id == shirtId condition beside the if isPurchased

If you want multiple ids to increment the IntValue, then you can have a table of Ids, and when a purchase is done, check if the id is in the table via table.find

3 Likes

The local you defined as local shirtId = 8695108536 is a local and only exists in this script (i.e. this is global and can be used by any part of this script). It is not the same thing that is passed to the function that you defined here:

game:GetService("MarketplaceService").PromptPurchaseFinished:Connect(function(player, shirtId, isPurchased).

This is because another piece of code (i.e. maybe not here) is calling this with a bunch of stuff that it sends to the function (these are not global but local to the function and only exist while the function is active). So shirtId (although it has the same name as the global one you declared at the top) is not the same variable. This is called scope, i.e. the function has a scope between the start and end of the function, but not the entire script although it can access the globals defined at the top (that is why they are called globals). So anything that is passed into the function only exists while the function is running, once it returns everything that was passed is locally destroyed (i.e. in this script) but not by the calling code that activated this function. That is why you must name inputs to functions differently to local variables defined in the same script as the function. WoTrox has already showed this in action by renaming the inputs to the function to be different to your local variable names (i.e. change the function input from shirtId to Id). Then there is no confusion about what variable you are working with. I hope this makes sense, search for variable scope on Google and you may get some examples that will describe it better.

1 Like