Issue with saving a table

So basically I don’t understand wath is wrong in my saving data script, if you could explain me it could be great.

local DTS = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local TestStore = DTS:GetDataStore("Test3")

function PlayerAdded(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	
	local Data = TestStore:GetAsync(player.UserId)
	if Data then
		Coins.Value = Data[1]
	else
		print("No data")
	end
end

function PlayerRemoving(player)
	local data = {}
	
	for i, v in ipairs(player:WaitForChild("leaderstats"):GetChildren()) do
		table.insert(data, v.Value)
	end
	print(data)
	
	local succ, err = pcall(function()
		TestStore:SetAsync(player.UserId, data)
	end)
	if err then
		print(err)
		print("failed")
	end
end

Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)

In the output there is nothing except the “No data”

Replace your current code with this one:

local DTS = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local TestStore = DTS:GetDataStore("Test3")

function PlayerAdded(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	
	local suc, res = pcall(function()
		return TestStore:GetAsync(player.UserId)
	end)

	Coins.Value = suc and res[1] or 0
end

function PlayerRemoving(player)
	local data = {}
	
	for i, v in player.leaderstats:GetChildren() do
		data[v.Name] = v.Value
	end
	
	local suc, res = pcall(function()
		return TestStore:SetAsync(player.UserId, data)
	end)

	if not suc then
		warn("Error saving data for player " .. player.Name .. ": " .. res)
	end
end

Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)

Also you don’t have to use ipairs/pairs in a for loop:

for i, v in ipairs(player.leaderstats:GetChildren()) do
	table.insert(data, v.Value)
end

To

for i, v in player.leaderstats:GetChildren() do
	table.insert(data, v.Value)
end

You need a BindToClose() function. The game doesn’t have enough time to save the data before it closes.

local runService = game:GetService("RunService")

game:BindToClose(function()
    if runService:IsStudio() then task.wait(5) return end
    for _, player in ipairs(Players:GetPlayers()) do
        PlayerRemoving(player)
    end
    task.wait(3)
end)

extra note: always use a pcall() when accessing the DataStore, it’s a network request so it can fail.

1 Like

It print an error in the output at this:

Coins.Value = suc and res[1] or 0

saying that: “attempt to index nil with number”

I didn’t tested the code, give me 1 second

ok but where do i put your code ? In the PlayerRemoving function ?

Just add it at the bottom of the script, below your PlayerAdded and PlayerRemoving conenctions.

Here try this:

local DTS = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local TestStore = DTS:GetDataStore("Test3")

function PlayerAdded(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	
	local suc, res = pcall(function()
		return TestStore:GetAsync(player.UserId)
	end)
	print(suc, res)
	Coins.Value = (suc and res and res.Coins) or 0
end

function PlayerRemoving(player)
	local data = {}
	
	for i, v in player.leaderstats:GetChildren() do
		data[v.Name] = v.Value
	end
	
	local suc, res = pcall(function()
		return TestStore:SetAsync(player.UserId, data)
	end)

	if not suc then
		warn("Error saving data for player " .. player.Name .. ": " .. res)
	end
end

Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)

didn’t work but printed in the output the right amount however the code from 12345oip work perfectly

1 Like

One more thing! I forgot one very important task.wait(5) after the loop. I updated it in the original post. Please add it in!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.