I am making a clothing store/Homestore and In the game, there’s a leaderboard/leader stats with purchases. Where whenever you purchase a piece of clothes it goes up by +1. When you did not buy any clothes from the group/game you should start with 0 and whenever you make a purchase from the group or game it should go up by +1
When I ad clothing IDs to the script it will go up which is a good thing as I am own them but it should not be going up on whoever joins because they do not own the clothes.
Scripts:
(Both scripts in SSS)
Purchase Leaderstats:
game.Players.PlayerAdded:connect(function(player)
local stats = Instance.new("IntValue")
stats.Name = "leaderstats"
stats.Parent = player
local purchases = Instance.new("IntValue")
purchases.Name = "Purchases"
purchases.Value = 0
purchases.Parent = stats
end)
PurchaseHandler Script:
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local PlayerOwnsAsset = MarketplaceService.PlayerOwnsAsset
local clothingid = {
123456789,
987654321,
101010101,
000000000,
}
Players.PlayerAdded:Connect(function(player)
for clothes in ipairs(clothingid) do
local success, doesPlayerOwnAsset = pcall(PlayerOwnsAsset, MarketplaceService, player, clothingid)
if doesPlayerOwnAsset then
player.leaderstats.Purchases.Value = player.leaderstats.Purchases.Value + 1
end
end
end)
For the 2nd script, did you mean to do this?
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local PlayerOwnsAsset = MarketplaceService.PlayerOwnsAsset
local clothingid = {
123456789,
987654321,
101010101,
000000000,
}
Players.PlayerAdded:Connect(function(player)
for _,clothId in ipairs(clothingid) do
local success, doesPlayerOwnAsset = pcall(PlayerOwnsAsset, MarketplaceService, player, clothId)
if success and doesPlayerOwnAsset then
player.leaderstats.Purchases.Value += 1
end
end
end)
You were giving it a table in the pcall rather than an id, also I think it’s better to check if the pcall was successful and the return instead of the return only
Someone else made me this script a while ago. I don’t script because I don’t know how too.
MarketplaceService.PlayerOwnsAsset confuses me, shouldn’t it be MarketplaceService:PlayerOwnsAsset(player_instance,assetid)
?
Try out if my change did the trick, it should as it checks for both the pcall to be successful and the return, and using an id rather than the whole table, although not sure if it’s needed to be pcalled, but I guess better safe than sorry
Just tried it out. When a player joins who owns no clothes the leaderstats start at 0, when they purchase it, it does not go up but if you rejoin then it goes up. How would I fix that?
Well there’s no code that checks if a purchase was finished, to do that, you need to use PromptPurchaseFinished
, I think an addition like to this what you have should work for you
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local PlayerOwnsAsset = MarketplaceService.PlayerOwnsAsset
local clothingid = {
123456789,
987654321,
101010101,
000000000,
}
Players.PlayerAdded:Connect(function(player)
for _,clothId in ipairs(clothingid) do
local success, doesPlayerOwnAsset = pcall(PlayerOwnsAsset, MarketplaceService, player, clothId)
if success then
player.leaderstats.Purchases.Value += 1
end
end
end)
MarketplaceService.PromptPurchaseFinished:Connect(function(player, id, purchased)
if not purchased then return end
if table.find(clothingid,id) then
player.leaderstats.Purchases.Value += 1
end
end)
If they didn’t purchase anything and the id is in the table, add 1. I really recommend you look through the api to find what you need and try to figure out how to do it/learn a bit of scripting as well!