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

Datastore Money Value is not saving correctly, any help?

Asked by 4 years ago

I have this modified script mostly inspired by Youtube and it doesn't fully work. The Kills value saves perfectly, unlike the Money. What happens is that after a few hours to days, most of the total Money earned will reset or decrease about 80-90%. Being a nearly essential part of the game, I believe resetting the value will just erase everybody's total progress and possibly encounter the problem again. This is a trend in all players that exceed 1000 in cash. I currently have more than 2000 kills, and I should have three times the amount in cash, however my data just erased several times and I currently have about 450 Money.

As I got this tutorial on how to make it from YT, I am not sure how to troubleshoot or fix this.

Note: Money is gained from killing players and NPCs, and is used on things to buy. Kills are just collected.

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("MoneySaveSystem")
local ds2 = datastore:GetDataStore("KillsSaveSystem")

game.Players.PlayerAdded:Connect(function(plr)
    local folder = Instance.new("Folder", plr)
    folder.Name = "leaderstats"
    local money = Instance.new("IntValue", folder)
    money.Name = "Money"
    local kills = Instance.new("IntValue", folder)
    kills.Name = "Kills"

    money.Value = ds1:GetAsync(plr.UserId) or 0
    ds1:SetAsync(plr.UserId, money.Value)

    kills.Value = ds2:GetAsync(plr.UserId) or 0
    ds2:SetAsync(plr.UserId, kills.Value)

    money.Changed:connect(function()
        ds1:SetAsync(plr.UserId, money.Value)
    end)

    kills.Changed:connect(function()
        ds2:SetAsync(plr.UserId, kills.Value)
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, errormessage = pcall(function()
        ds1:SetAsync(player.UserId.."-money",player.leaderstats.Money.Value)
    end)

    if success then
        print ("Player Data successfully saved.")
    else
        print ("Error in saving Player Data.")
        warn (errormessage)
    end
end)

1 answer

Log in to vote
0
Answered by 4 years ago

Quite simple fix, you just mixed up the keys.

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("MoneySaveSystem")
local ds2 = datastore:GetDataStore("KillsSaveSystem")

game.Players.PlayerAdded:Connect(function(plr)
    local folder = Instance.new("Folder", plr)
    folder.Name = "leaderstats"
    local money = Instance.new("IntValue", folder)
    money.Name = "Money"
    local kills = Instance.new("IntValue", folder)
    kills.Name = "Kills"

    money.Value = ds1:GetAsync(plr.UserId) or 0
    ds1:SetAsync(plr.UserId, money.Value)

    kills.Value = ds2:GetAsync(plr.UserId) or 0
    ds2:SetAsync(plr.UserId, kills.Value)

    money.Changed:connect(function()
        ds1:SetAsync(plr.UserId, money.Value)
    end)

    kills.Changed:connect(function()
        ds2:SetAsync(plr.UserId, kills.Value)
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, errormessage = pcall(function()
        ds1:SetAsync(player.UserId, player.leaderstats.Money.Value)
    end)

    if success then
        print ("Player Data successfully saved.")
    else
        print ("Error in saving Player Data.")
        warn (errormessage)
    end
end)

For the player removing, you had it as a different key than the player added.

0
If this still causes you problems then I don't know how to fix them, as I've never seen a datastore script formatted like this. ParticularlyPenguin 71 — 4y
0
Thank you, I was going into this semi-blind and hope it is simply my mistake. I will let you know if this works. Bgonzalezseeya11 27 — 4y
0
The fix did not solve the problem, but it did reduce how much money as lost. Thanks for your help. Bgonzalezseeya11 27 — 4y
Ad

Answer this question