I'm making a datastore that saves both money and gears. I started testing the datastore by seeing if it saved money, and it didn't save money. When the players had no save data, though, it set their money value to 520 like the script should do.
Basically my issue is that the script always sets money's value to 520, even if I change the money value to something like 1000 and leave the game. I want it to set the money value to 1000 when I leave the game, not 520. Here's my scipt:
local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("ToolSave")
local ds2 = DataStoreService:GetDataStore("MoneySave")
game.Players.PlayerAdded:connect(function(Player) --When the player joins the game, load value.
local Stats = Instance.new('Model')
Stats.Name = "leaderstats"
local Money = Instance.new('NumberValue',Stats)
Money.Name = "Money"
Stats.Parent = Player
--load data
local moneyData;
local toolData;
local CanSave = Instance.new('BoolValue', Player)
CanSave.Name = "CanSaveData"
CanSave.Value = true
local DataFetchSuccess, ErrorMessage = pcall(function()
moneyData = ds2:GetAsync(tostring(Player.UserId))
toolData = ds:GetAsync(tostring(Player.UserId))
end)
if DataFetchSuccess then
if moneyData ~= nil then --This could be where the problem is
Money.Value = moneyData
else
Money.Value = 520 -- Where money value is changed to 520
end
print(Money.Value)
if toolData ~= nil then
for i,v in pairs(toolData) do
print(v)
local tool = game.ServerStorage.Tools:FindFirstChild(v)
if toolData then
toolData:Clone().Parent = Player:WaitForChild("Backpack")
toolData:Clone().Parent = Player:WaitForChild("StarterGear")
end
end
end
else
Player.CanSaveData.Value = false
Player:Kick("Your data failed to load! Please rejoin.")
end
end)
game.Players.PlayerRemoving:connect(function(Player) -- When the player leaves the game, data must save.
if Player.CanSaveData.Value == false then return end - -[[There could also be a problem with saving the money--]]
local moneyData = Player.leaderstast.Money
local toolsToSave = {}
for i,v in pairs (Player.Backpack:GetChildren()) do
if v then
table.insert(toolsToSave, v.Name)
end
end
local DataWriteSuccess, ErrorMessage = pcall(function()
ds:SetAsync(tostring(Player.UserId), toolsToSave)
ds2:SetAsync(tostring(Player.UserId), moneyData)
end)
if not DataWriteSuccess then
local Retry_Count = 0
while Retry_Count < 6 do
wait(60)
local Succeed, Error = pcall(function()
ds:SetAsync(tostring(Player.UserId), toolsToSave)
ds2:SetAsync(tostring(Player.UserId), moneyData)
end)
if Succeed then break end
Retry_Count = Retry_Count + 1
end
end
end)
Thank you!

9 comments
Yeah, I noticed that too and fixed it. It's still not working
If I type print(moneyData) in the pcall function and right after it's declared as a variable, it shows nil. If I print it after the function, though, it just prints nothing.
I edited the answer, try that
Also, you typed "leaderstats" as "leaderstast" on line 55
So I'm supposed to load the money instance instead of the value? Or save the value and load the value?
edited again
Alright, I fixed that (I figured it out after looking at the script for a few minutes) & it's still not working for some reason. I'm using prints to try finding the issue, but I can't find it.
I changed the answer to include a few other things. Try that code out and let me know the results.
Thanks for your help! It works