Log in to vote

Any ideas why the leaderboard stats do not save when a shop purchase is made?

Asked by 11 months ago

Hi. I am having an issue with data store. I have made a datastore script that saves when you enter and leave. This works fine, however when I purchase something with the shop, it works fine up until I rejoin the game where the points initially deducted from the shop have came back. For example, if I have 30 coins, I purchase an item for the shop for 10 coins. I now have 20 coins. However, when I rejoin, I once again have 30 coins. Any help with this would be appreciated.

DataStore script

local DataStore = game:GetService("DataStoreService"):GetDataStore("leaderStorage")


    local stats ="IntValue",player)
    stats.Name = "leaderstats"

    local Wins ="IntValue",stats)
    Wins.Name = "Wins"

    local Coins ="IntValue",stats)
    Coins.Name = "Coins"

    local key = "player-"..player.userId

    local savedValues = DataStore:GetAsync(key)

    if savedValues then
        -- save format: (wins,coins)
        Wins.Value = savedValues[1]
        Coins.Value = savedValues[2]
        local valuesToSave = {Wins.Value, Coins.Value}


    local key = "player-"..player.userId

    -- save key(wins, coins)
    local valuesToSave = {player.leaderstats.Wins.Value, player.leaderstats.Coins.Value}
    DataStore:SetAsync(key, valuesToSave)

Shop script (game>StarterGui(PlayerGui)>ScreenGui>Frame>TextButton>LocalScript(Shop Script)) ItemName and Price are children of the TextButton, ItemName is a string value while Price is an integer value.

local player = game.Players.LocalPlayer
local leaderboard = player:WaitForChild("leaderstats")
local button = script.Parent
local price = button:WaitForChild("Price")
local item = button:WaitForChild("ItemName")
local rs = game:GetService("ReplicatedStorage")

    if player.ItemStorage:FindFirstChild(item.Value)or player.Character.Torso:FindFirstChild(item.Value)then
        leaderboard.Coins.Value >= price.Value then
        leaderboard.Coins.Value = leaderboard.Coins.Value - price.Value
        local item = rs:FindFirstChild("PurchasableItems"):WaitForChild(item.Value)
        item:Clone().Parent = player.StarterGear
        item:Clone().Parent = player.Character.Torso

1 answer

Log in to vote
Answered by 11 months ago

If your game is filtering enabled, the price decrease would only apply to the localplayer instead of the rest of the players and server. Because it doesn't apply to the server, it doesn't save.

To fix this, you can use a remove event. Let's say you have a remote event named "Purchased" in replicated storage. Create a server side script and put the following in it.

    local Coins = player.leaderstats.Coins
    Coins.Value = Coins.Value - Cost

This script finds the remove event in replicated storage and connects a function when the event is fired.

In the localscript (shop script) put the following code.

local Event = game.ReplicatedStorage.RemoteEF:FindFirstChild("Purchased")
        Event:FireServer("Nameofobjectbeingpurchased", price)

Put that code in the script when you purchase something, for example after the script checks if they have enough money it will fire the server with parameters of the name of the object and price.

Thanks! Draebrewop 45 — 11mo

Answer this question