Purchased Leaderboard Help

Hello,
So basically I want a purchases leaderboard, but I don’t know how to do it.

If there is a model please link it to me! :wink:

2 Likes

You would create a intValue inside of leaderstats, whenever they purchase an item it would add one onto it. Let me show you an example:

Inside a Script in ServerScriptService:

game:GetService("Players").PlayerAdded:Connect(function(player)
        local leaderstats = instance.new("Folder"); leaderstats.Parent = player
        local Purchase = instance.new("IntValue")
        Purchase.Name = "Purcahses"; Purchase.Parent = leaderstats
end)

This creates the leaderstats value, now simply whenever a purchase is made use a remote event from the client to the server to update the Purchases Value like this:

[EventLocation].OnServerEvent:Connect(function(player)
     player.leaderstats.Purchases.Value += 1
end)
4 Likes

Same thing as I was about to say in your previous post. Do you already have the leaderstats made? Are you making it increase when you buy a developer product or an ingame item?

No, I don’t know how to do this. When you buy clothing and you purchase it. It will add 1+

Does it save? Like if you rejoin the game will you still have the amount of purchases you had before. :sob: I can’t explain things properly…

@TheDCraft So do I create a RemoteEvent?

game.Players.PlayerAdded:Connect(function(a) --// 'a' is the player
    local folder = Instance.new("Folder", a)
    folder.Name = "leaderstats" --// This cannot be changed.

    local purchases = Instance.new("IntValue", folder)
    purchases.Value = 0
    exampleEvent.OnServerEvent:Connect(function()
        purchases.Value = purchases.Value + 1
    end)
end)

Then you could fire the event each time a purchase gets handled, to work on how to make sure it’s confirmed go to the bottom of this DevHub page:

To Do what you require, you will need to create the leaderstats, create values, do something when you a buy a shirt/pants using PromptPurchaseFinished.

For saving you require to use datastoring. I typically use DataStore2 and is the one I know how to use, but if you want to use something else, you’ll have to do your research.


In ServerScriptService, add a script with this code
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
    local leaderstats = instance.new("Folder")
    leaderstats.Parent = player

    local Purchase = instance.new("IntValue")
    Purchase.Name = "Purchases"
    Purchase.Parent = leaderstats
end)

This handles the creation of the leaderboard, now for the clothing code, for this example we’ll make it work for one thing. In a part with a clickdetector, add this code

local part = script.Parent
local detector = part.ClickDetector
local mps = game:GetService("MarketPlaceService") --The primary service for purchases

detector.MouseClick:Connect(function(plr) --MouseClick passes the player who clicked it
    mps:PromptPurchase(plr, IDOFSHIRT, false) --Replace IDOFSHIRT with your shirt id
end)

Now in another script in ServerScriptService, add this

local mps = game:GetService("MarketPlaceService")

mps.PromptPurchaseFinished:Connect(function(plr,id,purchased) --This event also runs when you cancel the purchase
    if not purchased then return end --Do nothing if it wasn't purchased
    plr.leaderstats.Purchases.Value += 1 --Add 1 to the value of Purchases
end)

This should be enough to get your started, you’ll have to do some reason as to how to datastore it, so I’m not just a full code giver so you can also learn as well!

Saves:

Inside a Script in ServerScriptService:

local Data = game:GetService("DataStoreService"):GetDataStore("TheDS1")
local Players = game:GetService("Players")
local Connections = {}
local SpeedOfUpdate = 0; -- Time in seconds between value updates.

local function PlayerAdded(Player)
	local Success, Stats, Error = pcall(function() return Data:GetAsync(tostring(Player.UserId)) end)
	if not Success and Error then
		warn(Error)
	else
		local StatFolder = Instance.new("Folder")
		StatFolder.Name = "leaderstats"
		local Stat1 = Instance.new("NumberValue")
		Stat1.Name = "Purchases"
		
		Stat1.Value = Stats and Stats[1] or 0
		
		Stat1.Parent = StatFolder
		StatFolder.Parent = Player
	end
end

Players.PlayerAdded:Connect(PlayerAdded)

Players.PlayerRemoving:Connect(function(Player)
	if Connections[Player] then Connections[Player]:Disconnect() end
	local Folder = Player:FindFirstChild("leaderstats")
	local Success, Error = pcall(function() 
		Data:SetAsync(tostring(Player.UserId),{
			Folder.Purchases.Value,
		})
	end)
	if not Success then
		warn(Error)
	else
		Folder:Destroy()
	end
end)

game:BindToClose(function()
	wait(1)
end)

Another Script inside ServerScriptService:

-- Make a RemoteEvent
[EventLocation].OnServerEvent:Connect(function(player,assetId,isPurchased)
     player.leaderstats.Purchases.Value += 1
end)

Local Script:

local MarketplaceService = game:GetService("MarketplaceService")
 
MarketplaceService.PromptPurchaseFinished:Connect(function(player, assetId, isPurchased)
     if isPurchased then
          [EventLocation]:FireServer(player,assetId,isPurchased)
     end
end)

Sorry I had to do something, @EmbatTheHybrid @DevCramp has the way to save and create it.

I wanted to comment on every single line explaining what it does but it’s taking too long so I decided to fully delete the comments.

There would be 2 ways for this.

In-game purchases (will only increase when you purchase a product in the game):

--Variables--
local playerSV = game:GetService("Players")
local marketplaceSV = game:GetService("MarketplaceService")
local datastoreSV = game:GetService("DataStoreService")
local purchasedatastore = datastoreSV:GetDataStore("PurchaseDatastore")

--Script--
playerSV.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player 
	
	local purchasesvalue = Instance.new("IntValue")
	purchasesvalue.Name = "Purchases"
	local success = pcall(function()
		local purchasedata = purchasedatastore:GetAsync(player.UserId)
		
		if purchasedata then
			purchasesvalue.Value = purchasedata
		else
			purchasesvalue.Value = 0
		end
	end)
	
	if not success then
		purchasesvalue.Value = 0
	end
	purchasesvalue.Parent = leaderstats
end)

playerSV.PlayerRemoving:Connect(function(player)
	local purchasesvalue = player:WaitForChild("leaderstats"):WaitForChild("Purchases")
	
	pcall(function()
		purchasedatastore:SetAsync(player.UserId, purchasesvalue.Value)
	end)
end)

game:BindToClose(function()
	for i, player in ipairs(playerSV:GetPlayers()) do
		local purchasesvalue = player:WaitForChild("leaderstats"):WaitForChild("Purchases")

		pcall(function()
			purchasedatastore:SetAsync(player.UserId, purchasesvalue.Value)
		end)
	end
end)

marketplaceSV.PromptPurchaseFinished:Connect(function(player, id, purchased)
	if purchased then
		local purchasesvalue = player:WaitForChild("leaderstats"):WaitForChild("Purchases")
		
		purchasesvalue.Value += 1
	end
end)

Total purchases (will increase when you purchase something, doesn’t matter if it’s in-game or on the website):

--Variables--
local playerSV = game:GetService("Players")
local marketplaceSV = game:GetService("MarketplaceService")
local purchaseidtable = {} --Put all the ID's of your products in here. Example: {000000, 111111}.--

function updatepurchases(player)
	local purchasesvalue = player:WaitForChild("leaderstats"):WaitForChild("Purchases")
	
	for i, purchaseid in ipairs(purchaseidtable) do
		if marketplaceSV:PlayerOwnsAsset(player, purchaseid) then
			purchasesvalue.Value += 1
		end
	end
end

--Script--
playerSV.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player 
	
	local purchasesvalue = Instance.new("IntValue")
	purchasesvalue.Name = "Purchases"
	updatepurchases(player)
	purchasesvalue.Parent = leaderstats
end)

--Note: I don't think theres an event for when a player purchased something outside of the game. If there is please let me know. If you still wanna refresh stuff you could use a while wait(1) loop and then for ipairs do with all the players. Reply to me for more info if you'd like.--
6 Likes