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

leaderstats does not add value. How can i fix it?

Asked by 3 years ago

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

0
This has a big flaw. You don't have to actually purchase the item, you just have to click it. You will still have a bigger purchase count if you press "cancel" User#30567 0 — 3y
0
so how would it only add value when bought? loner_cactus 0 — 3y

3 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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:

  • Wrapped all datastore-related things in pcall
  • Used threads to only have one playerAdded function

Minor changes:

  • Changed names of items
  • Capitalized all connect()s

Mannequin 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:

  • Actually checks whether player owns item or not

Minor changes:

  • Redefined function
0
tysm! loner_cactus 0 — 3y
0
No problem! User#30567 0 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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.

0
First of all tysm for answering, second what do i do if both dont work? btw its line 6. loner_cactus 0 — 3y
Log in to vote
0
Answered by 3 years ago

You should always pcall when using datastores, from what I've heard.

0
idk how but my datastore works its just my leaderstats giving me trouble loner_cactus 0 — 3y

Answer this question