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!
local DataStoreService = game:GetService("DataStoreService") local ds = DataStoreService:GetDataStore("ToolSave") local ds2 = DataStoreService:GetDataStore("MoneySave") game:GetService("Players").PlayerAdded:Connect(function(Player) --When the player joins the game, load value. local Stats = Instance.new('Folder') 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 print("Money save found") Money.Value = moneyData else print("No money save found.") 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:GetService("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.leaderstats.Money.Value local toolsToSave = {} print(MoneyData) print(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() print("Saving "..Player.Name.."s stats") 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() print("Attempting to save "..Player.Name.."'s stats.") ds:SetAsync(tostring(Player.UserId), toolsToSave) ds2:SetAsync(tostring(Player.UserId), moneyData) end) if Succeed then print("Saved "..Player.Name.."'s stats") break end Retry_Count = Retry_Count + 1 end end end)