How can I save a table to my data store

I’m trying to save a table of all a players trials do my data store but it wont work no matter what I try how am I meant to do this here is my script

local dsService = game:GetService("DataStoreService")
local ds = dsService:GetDataStore("Steps")
local ds2 = dsService:GetDataStore("Trails")
local ds3 = dsService:GetDataStore("Level")
local ds4 = dsService:GetDataStore("Xp")

game.Players.PlayerAdded:Connect(function(player)

	local folder = Instance.new("Folder", player)
	folder.Name = "leaderstats"
	local trails = Instance.new("IntValue", player)
	trails.Name = "Trails" 
	local steps = Instance.new("IntValue", folder)
	steps.Name = "Steps"
	local level = Instance.new("IntValue", folder)
	level.Name = "Level"
	local xp = Instance.new("IntValue", player)
	xp.Name = "Xp" 
	local twopass = Instance.new("BoolValue", player)
	twopass.Name = "TwoPass" 
	local threepass = Instance.new("BoolValue", player)
	threepass.Name = "ThreePass" 

	
	steps.Value = ds:GetAsync(player.UserId) or 0
	trails.Value = ds2:GetAsync(player.UserId) or 0
	level.Value = ds3:GetAsync(player.UserId) or 0
	xp.Value = ds4:GetAsync(player.UserId) or 0

end)


game.Players.PlayerRemoving:Connect(function(player)

	local steps = player.leaderstats.Steps
	local trails = player.Trails
	local level = player.leaderstats.Level
	local xp = player.Xp
	ds:SetAsync(player.UserId, steps.Value)
	ds2:SetAsync(player.UserId, trails.Value)
	ds3:SetAsync(player.UserId, level.Value)
	ds4:SetAsync(player.UserId, xp.Value)
end)

Are “Access To API Services” is Turn On In Game Settings?

try this:

function setData(player)
	local data = {}
	for i, v in pairs(player.leaderstats:GetChildren()) do
		if v then
			data[v.Name] = v.Value
		end
	end
	return data
end

game.Players.PlayerAdded:Connect(function(player)
	local data = ds:GetAsync(player.UserId)
	local dataTable = setData(player)
	
	local folder = Instance.new("Folder", player)
	folder.Name = "leaderstats"
	local trails = Instance.new("IntValue", folder)
	trails.Name = "Trails" 
	local steps = Instance.new("IntValue", folder)
	steps.Name = "Steps"
	local level = Instance.new("IntValue", folder)
	level.Name = "Level"
	local xp = Instance.new("IntValue", player)
	xp.Name = "Xp" 
	local twopass = Instance.new("BoolValue", player)
	twopass.Name = "TwoPass" 
	local threepass = Instance.new("BoolValue", player)
	threepass.Name = "ThreePass" 
	
	
	if data then
		xp.Value = dataTable["Xp"]
		level.Value = dataTable["Level"]
		steps.Value = dataTable["Steps"]
		trails.Value = dataTable["Trails"]
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local data = setData(player)
	local success, error = pcall(function()
	    ds:SetAsync(player.UserId, data)
    end)
end)

Btw i moved Trails into the leaderstats folder

One thing I would do along with that is use

HttpService:JSONEncode()

and then use

HttpService:JSONDecode()

to retrieve the data so that you are saving a string to the datastore which is more efficient.

You have misunderstood me i want to leave everything else how it is but be able to save a data table to the trails value

-- setasync
datastore:SetAsync(userid, {
   value1 = 0,
   value2 = 0,
   value3 = 0
})


-- getasync
local data = datastore:GetAsync(userid);
data.value1
data.value2
data.value3

u also need to use pcall

1 Like

Don’t really know how to use this

local datastore = game:GetService("DataStoreService"):GetDataStore("datastoreName")

game.Players.PlayerAdded:Connect(function(p)
	local folder = Instance.new("Folder", player)
	folder.Name = "leaderstats"
	local value1 = Instance.new("IntValue", folder)
	value1.Name = "Value1"
	local value2 = Instance.new("IntValue", folder)
	value2.Name = "Value2"
	local value3 = Instance.new("IntValue", folder)
	value3.Name = "Value3"
	
	local suc,err = pcall(function()
		local data = dataStore:GetAsync(p.UserId)
		value1.Value = data.val1 or 0
		value2.Value = data.val2 or 0
		value3.Value = data.val3 or 0
	end)
	if err then
		warn(err)
	else
		warn("successfully loaded player's data")
	end
end)

game.Players.PlayerRemoving:Connect(function(p)
	local suc,err = pcall(function()
		datastore:SetAsync(p.UserId, {
		val1 = p.leaderstats.Value1.Value
		val2 = p.leaderstats.Value2.Value
		val3 = p.leaderstats.Value3.Value
		}
	end)
	if suc then
		warn("Successfully saved player's data")
	else
		warn(err)
	end
end)

did my script work? just wondering