Data store not working

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

Any help would be very apreciated!

Are you testing this in studio if so make sure that access to api services are turned on

Yes i’m testing this on studio and i have already enabled access to api services

And by the way the second script is a local script

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.

Hope this helps. :wink:

1 Like

Oh ok thx i’ll see what i can do.

You should also consider moving your auto save script to run on the server. Otherwise, a player can change their money to 9,999,999.

Oh ok thx so much for the help i rlly appreciate it

How can i make a table and save it?

Pls anyone knows how to do this?

Just like how you did here. :slight_smile:

Ok but should i delete the autosave?

What do you mean by “deleting the autosave”?

I just don’t understand should i do this :

-- 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)

Is the second script a LocalScript? LocalScripts cannot save data.

I changed it to a server sided script

In your new script, when a player is leaving, you only insert data into a table, but don’t save it.

Yea that’s why i’m asking how to save it do you know how?

Yes, it’s similar to what you do to get it.

local success, err = pcall(function()
  YourDataStore:SetAsync(Player.UserId..'-key', Data)
end)