Im making a leaderstats for clothes purchased and time spent. Time spent works but purchase count doesn't.
local datastore = game:GetService("DataStoreService") local ds1 = datastore:GetDataStore("TimeSaveSystem") local ds2 = datastore:GetDataStore("PurchaseCountSaveSystem") game.Players.PlayerAdded:connect(function(plr) local folder = Instance.new("Folder", plr) folder.Name = "leaderstats" local Time = Instance.new("IntValue", folder) Time.Name = "Time" local PurchaseCount = Instance.new("IntValue", folder) PurchaseCount.Name = "PurchaseCount" Time.Value = ds1:GetAsync(plr.UserId) or 0 ds1:SetAsync(plr.UserId, Time.Value) PurchaseCount.Value = ds2:GetAsync(plr.UserId) or 0 ds2:SetAsync(plr.UserId, PurchaseCount.Value) Time.Changed:connect(function() ds1:SetAsync(plr.UserId, Time.Value) end) PurchaseCount.Changed:connect(function() ds2:SetAsync(plr.UserId, PurchaseCount.Value) end) end) local function addTime(player) while true do wait(60) player.leaderstats.Time.Value = player.leaderstats.Time.Value + 1 end end game.Players.PlayerAdded:Connect(addTime)
you buy the clothes from a mannequin with this code (not a local script)
local CLICK_BLOCK = script.Parent local ITEM_ID = 5333151672 --ID number of shirt local Click = Instance.new("ClickDetector",CLICK_BLOCK) Click.MouseClick:connect(function(p) game:GetService("MarketplaceService"):PromptPurchase(p,ITEM_ID) player.leaderstats.PurchaseCount.Value = player.leaderstats.PurchaseCount.Value + 1 end)
tysm for helping
Taking suggestions from everybody here, I made a final fix, with explanations.
Main script:
local datastore = game:GetService("DataStoreService") local TimeSave = datastore:GetDataStore("TimeSaveSystem") local PurchaseCount = datastore:GetDataStore("PurchaseCountSaveSystem") local function SaveData(Player) local folder = Instance.new("Folder", Player) folder.Name = "leaderstats" local Time = Instance.new("IntValue", folder) Time.Name = "Minutes Played" local PurchaseCount = Instance.new("IntValue", folder) PurchaseCount.Name = "Items Purchased" spawn(function() while true do wait(60) Time.Value = Time.Value + 1 end end) local TimeError, TimeData = pcall(function() Time.Value = TimeSave:GetAsync(Player.UserId) or 0 end) while not TimeError do TimeError, TimeData = pcall(function() Time.Value = TimeSave:GetAsync(Player.UserId) or 0 end) wait(5) end local PurchaseCountError, PurchaseCountData = pcall(function() PurchaseCount.Value = PurchaseCount:GetAsync(Player.UserId) or 0 end) while not PurchaseCountError do PurchaseCountError, PurchaseCountData = pcall(function() PurchaseCount.Value = PurchaseCount:GetAsync(Player.UserId) or 0 end) wait(5) end Time.Changed:Connect(function() local TimeSaveError, TimeSaveData = pcall(function() TimeSave:SetAsync(Player.UserId, Time.Value) end) while not TimeSaveError do TimeSaveError, TimeSaveData = pcall(function() TimeSave:SetAsync(Player.UserId, Time.Value) end) end end) PurchaseCount.Changed:Connect(function() local PurchaseSaveError, PurchaseSaveData = pcall(function() PurchaseCount:SetAsync(Player.UserId, PurchaseCount.Value) end) while not PurchaseSaveError do PurchaseSaveError, PurchaseSaveData = pcall(function() PurchaseCount:SetAsync(Player.UserId, PurchaseCount.Value) end) end end) end game.Players.PlayerAdded:Connect(SaveData)
Major changes:
pcall
playerAdded
functionMinor changes:
connect()
sMannequin script:
local PantsClickDetector = script.Parent -- Put this in the ClickDetector local PantsID = 5358411386 -- Replace with pant id local MS = game:GetService("MarketplaceService") local function PurchaseItem(Player) local PurchaseCount = Player.leaderstats.PurchaseCount MS:PromptPurchase(Player, PantsID.Value) for i in 0, 5 do local Success, playerOwnsAsset = pcall(MS.PlayerOwnsAsset, MS, Player, PantsID.Value) if playerOwnsAsset then PurchaseCount.Value = PurchaseCount.Value + 1 end wait(5) end end PantsClickDetector.MouseClick:Connect(PurchaseItem)
Major changes:
Minor changes:
First off, in line 6 of the mannequin script :connect is deprecated, use :Connect instead. Test it, and if it still doesn't work, put the code that's in line 6 at the very end.
You should always pcall when using datastores, from what I've heard.