So i’ve made a leadersats script and made an atm system that auto saves but the data store just doesn’t save anything how can i fix this here is my leaderstats code :
-- leaderstats script --
local DataStore = game:GetService("DataStoreService"):GetDataStore("Test001")
local SaveRemote =
game:GetService("ReplicatedStorage"):WaitForChild("AtmSave")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
--1
local Kills = Instance.new("IntValue",leaderstats)
Kills.Name = "Kills"
Kills.Value = 0
--2
local Cash = Instance.new("IntValue", leaderstats)
Cash.Name = "Cash"
Cash.Value = 0
--3
local Inbank = Instance.new("IntValue", leaderstats)
Inbank.Name = "Inbank"
Inbank.Value = 0
local Success , ErrorText = pcall(function()
local GetData = DataStore:GetAsync(player.UserID)
for i , v in pairs(GetData) do
if i == 1 then
Kills.Value = GetData[i]
elseif i == 2 then
Cash.Value = GetData[i]
elseif i == 3 then
Inbank.Value = GetData[i]
end
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
DataStore:SetAsync(player.UserId, player.leaderstats.Kills.Value)
DataStore:SetAsync(player.UserId, player.leaderstats.Cash.Value)
DataStore:SetAsync(player.UserId, player.leaderstats.Inbank.Value)
end)
SaveRemote.OnServerEvent:Connect(function(player , val)
DataStore:SetAsync(player.UserId,val)
end)
here is the autosave script :
-- Autosave script --
local Remote = game:GetService("ReplicatedStorage"):WaitForChild("AtmSave")
local Data = game.Players.LocalPlayer:WaitForChild("leaderstats")
while true do
wait(30)
local SaveDataTable = {}
table.insert(SaveDataTable, 1 , Data.Kills.Value)
table.insert(SaveDataTable, 2 , Data.Cash.Value)
table.insert(SaveDataTable, 3 , Data.Inbank.Value)
end
Here, you are saving each value individually. This overrides the previous so you will only get “Inbank” saved. Try add them to a table and save that.
Back to your main question: Saving data when a player leaves doesn’t work in studio (at least from my experience). So fix up the mistake I listed above and then test your game in a normal Roblox server.
-- leaderstats script --
local DataStore = game:GetService("DataStoreService"):GetDataStore("Test001")
local SaveRemote =
game:GetService("ReplicatedStorage"):WaitForChild("AtmSave")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
--1
local Kills = Instance.new("IntValue",leaderstats)
Kills.Name = "Kills"
Kills.Value = 0
--2
local Cash = Instance.new("IntValue", leaderstats)
Cash.Name = "Cash"
Cash.Value = 0
--3
local Inbank = Instance.new("IntValue", leaderstats)
Inbank.Name = "Inbank"
Inbank.Value = 0
local Success , ErrorText = pcall(function()
local GetData = DataStore:GetAsync(player.UserID)
for i , v in pairs(GetData) do
if i == 1 then
Kills.Value = GetData[i]
elseif i == 2 then
Cash.Value = GetData[i]
elseif i == 3 then
Inbank.Value = GetData[i]
end
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
-- Here are the things that i changed : --
local SaveDataTable = {}
table.insert(SaveDataTable, 1 , Data.Kills.Value)
table.insert(SaveDataTable, 2 , Data.Cash.Value)
table.insert(SaveDataTable, 3 , Data.Inbank.Value)
end)
SaveRemote.OnServerEvent:Connect(function(player , val)
DataStore:SetAsync(player.UserId,val)
end)