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

Why my datastore is not saving my added money?

Asked by 2 years ago

I've made a datastore that saves money. The problem here is that when I click the button that gives me the money, the money gets added but when I exit and rejoin the money isn't there. I tested about adding the value through properties and the datastore saves that value. Here are the Scripts:

Leaderstats (script)

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("MyDataStore")

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

    local coins = Instance.new("IntValue")
    coins.Name = "Coins"
    coins.Parent = leaderstats

    local data
    local success, errorMessage = pcall(function()
        data = myDataStore:GetAsync(player.UserId)
    end)

    if success then
        coins.Value = data
    else
        warn(errorMessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, errorMessage = pcall(function()
        local data = myDataStore:SetAsync(player.UserId, player.leaderstats.Coins.Value)
    end)

    if success then
        print("Data Saved")
    else
        warn(errorMessage)
    end
end)

game:BindToClose(function()
    for i, player in pairs(game.Players:GetChildren()) do
        player:Kick("Server Closed")
    end

    wait(2)
end)

Button (localscript)

local Player = game.Players.LocalPlayer
local PlayerMoney = Player:WaitForChild('leaderstats'):WaitForChild('Coins')
script.Parent.MouseButton1Click:Connect(function()
PlayerMoney.Value += 1
end)

API is enabled too. I hope somebody helps me. Thank you in advance.

0
Are you testing in studio. If you are, are you actually leaving the game or just stopping it? Killerbot712 47 — 2y
0
I've tested both. Leaving and stopping the game. wackoscar1 0 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago
local key=""..player.UserId -- it needs to be a string
0
where I've to put it? wackoscar1 0 — 2y
0
On GetAsync & SetAsync : myDataStore:GetAsync(key) & myDataStore:SetAsync(key,player.leaderstats.Coins.Value) scripterhelper5354 896 — 2y
Ad

Answer this question