So, I am doing a clothing homestore…But currently I need a leaderstat where whenever a player buys a shirt it will add 1 point to purchased value in leaderstats, How could I possibly achieve that?
Will the Shirt be an asset that the person buys with Robux?
Yes, He will gain 1 everytime he buys a shirt in the homestore
Just connect the purchase success. So whenever the purchase is complete and he did buy it then just player:WaitForChild("leaderstats"):WaitForChild("YourPurchaseValue").Value += 1
That would take so long since there is like over 100 shirt, Do you think there is possibly a easier way?
A 100 items is nothing for modern computers and a single loop.
leaderstats
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local ShirtsPurchased = Instance.new("IntValue")
ShirtsPurchased.Name = "ShirtsPurchased"
ShirtsPurchased.Value = 0
ShirtsPurchased.Parent = leaderstats
end)
Gamepass (local script)
local MPC = game:GetService("MarketPlaceService")
local player = game.Players.LocalPlayer
local shirtId = 00000000
while wait() do
if MPC:PlayerOwnsAsset(player.UserId, shirtId) then
player:WaitForChild("leaderstats"):WaitForChild("ShirtsPurchased").Value += 1
break
end
end
A for i loop would be good if i am not wrong
That would only affect client and easy to be exploitable
Thanks, But is it possibly to make a table containing all the ids or something
You could use a for loop to loop through all the shirts and have a value inside them be a intValue that has the assetId inside and then just prompt the purchase.
for index, shirt in ipairs(workspace:WaitForChild("shirts"):GetChildren())
shirt.ClickDetector.MouseClick:Connect(x)
end
x being a function that will get the assetId out the Instance and prompting the purchase
If you want multiple shirtIds in a table.
local MPC = game:GetService("MarketPlaceService")
local player = game.Players.LocalPlayer
local shirtIds = {}
while wait() do
for i, id in pairs(shirtIds) do
if MPC:PlayerOwnsAsset(player.UserId, id) then
player:WaitForChild("leaderstats"):WaitForChild("ShirtsPurchased").Value += 1
break
end
end
end
Note : I haven’t tested this.
That will cause when you keep clicking it you will keep getting points though
game:GetService("Players").PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local purchase = Instance.new("IntValue")
purchase.Name = "Purchase Count"
purchase.Value = 0
purchase.Parent = leaderstats
end)
game:GetService("MarketplaceService").PromptPurchaseFinished:Connect(function(player, assetId, isPurchased)
if isPurchased then
print(player.Name .. " bought an item with AssetID: " .. assetId)
local leaderstats = player.leaderstats
local purchaseStat = leaderstats and leaderstats:FindFirstChild("Purchase Count")
if purchaseStat then
purchaseStat.Value += 1
end
else
return
end
end)
I would recommend this because it affect all purchases. Also if you want to make it a whitelist or blacklist you can do it via checking the assetid parameter.
Note: I haven’t tested this. Put that in script
Lot of thing’s taken from: here and here
All the logic to create the stats and the logic you need to buy clothes and apply them to the leader stats is provided by Dovg. These stats will not be prevalent through your game though, if you want these stats to be transient then the solution is already given in his post. If these stats need to be saved which I assume they will be if they paid Robux for them, you have the responsibility to write the leader stats into the Datastore for each player that buys them, and load these into the stats table when players are added to your game.
A good example of DataStore acces is shown in the “Move it Simulator” available in the example files Roblox provides when you create a new place. Load the example and navigate to ServerScriptService.Data.Datastore. It shows methods for showing how to save tables of values, which is definitely something you will need even if you are just offering one type of item to buy. You will need a table to store the shirt id’s they have purchased and access these when loading the stats using Dovg’s method. The example also has good methods of preventing data loss, and includes autosave capabilities, as well as manual saving.
Thank you so much, That was really helpful…Will definitely look through the game and see how it works!
Alright well I tested this and it works, tysm!
I updated the script to include datastore (saving when leaving/joining)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local MarketplaceService = game:GetService("MarketplaceService")
local purchasesDataStore = DataStoreService:GetDataStore("PlayerPurchases")
-- Function to load player data
local function loadPlayerData(player)
local success, data = pcall(function()
return purchasesDataStore:GetAsync(player.UserId)
end)
if success and data then
return data
else
return 0 -- Default value if no data exists
end
end
-- Function to save player data
local function savePlayerData(player)
local leaderstats = player:FindFirstChild("leaderstats")
local purchaseStat = leaderstats and leaderstats:FindFirstChild("Purchases")
if purchaseStat then
local success, err = pcall(function()
purchasesDataStore:SetAsync(player.UserId, purchaseStat.Value)
end)
if not success then
warn("Failed to save data for player " .. player.Name .. ": " .. err)
end
end
end
-- Function to create leaderstats
local function createLeaderstats(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local purchase = Instance.new("IntValue")
purchase.Name = "Purchases"
purchase.Value = loadPlayerData(player)
purchase.Parent = leaderstats
end
-- When a player joins the game, create leaderstats and load Purchases stat
Players.PlayerAdded:Connect(function(player)
createLeaderstats(player)
end)
-- Save player data when they leave
Players.PlayerRemoving:Connect(savePlayerData)
-- When a purchase is completed, check if it was successful and update the Purchases stat
MarketplaceService.PromptPurchaseFinished:Connect(function(player, assetId, isPurchased)
if isPurchased then
print(player.Name .. " bought an item with AssetID: " .. assetId)
local leaderstats = player:FindFirstChild("leaderstats")
local purchaseStat = leaderstats and leaderstats:FindFirstChild("Purchases")
if purchaseStat then
purchaseStat.Value += 1
savePlayerData(player) -- Save data immediately after purchase
end
end
end)