Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why doesn't my leaderstat update until I rejoin the game?

Asked by
JipSea 23
3 years ago

Hello! My leaderstat for Most purchased items in game is now working, but it doesn't update until I quit and rejoin the game-

Any advice or suggestions on what I can do to allow this script to update real time?

Thanks.

local marketPlaceService = game:GetService("MarketplaceService")
local playerOwnsAsset = marketPlaceService.PlayerOwnsAsset

local Players = game:GetService("Players")

local Clothes = {
    123456;
    1234567;    }

Players.PlayerAdded:Connect(function(Plr)
    local Statistics = Instance.new("Folder")
    Statistics.Name = "leaderstats"
    Statistics.Parent = Plr

    local Bought = Instance.new("IntValue")
    Bought.Name = "Bought"
    Bought.Value = 0
    Bought.Parent = Statistics

     for _, itemID in ipairs(Clothes) do
        local success, purchasedItem = pcall(playerOwnsAsset, marketPlaceService, Plr, itemID)

        if purchasedItem then
            Bought.Value = Bought.Value + 1
        end
    end
end)

Note: I used placeholders instead of the actual clothing ID's for demonstration purposes only. Any help would be greatly appreciated!

Thanks.

1 answer

Log in to vote
0
Answered by
Elyzzia 1294 Moderation Voter
3 years ago

you can use the PromptPurchaseFinished event to update the Bought value whenever the player purchases an item

marketPlaceService.PromptPurchaseFinished:Connect(function(player, assetId, wasPurchased)
    if wasPurchased and table.find(Clothes, assetId) then
        player.leaderstats.Bought.Value += 1
    end
end)
Ad

Answer this question