Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Datastore Money Value is not saving correctly, any help?

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

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.

3 comments

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.

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.

0

The fix did not solve the problem, but it did reduce how much money as lost. Thanks for your help.

This discussion is preserved as it was. New answers, comments, and votes are closed.