Data not loading as expected

  1. What do you want to achieve? Keep it simple and clear!
    I want the data to save properly as loading data returns as nil
  2. What is the issue? Include screenshots / videos if possible!
    Whenever data is attempted to load, data isnt a table, just nill.
players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats

	local weapons = Instance.new("Folder")
	weapons.Name = "Weapons"
	weapons.Parent = player

    local PlayerUserId = "player_"..player.UserId
	local Data 
	local success, err = pcall(function()
	   Data = ds:GetAsync(PlayerUserId)
	end)
	if success then
		coins.Value = Data["coins"]
		local weapons = Data["dataweapons"]
		for i, v in pairs(weapons) do
			v.Parent = player.Weapons
		end
	end
end)
	
	



game.Players.PlayerRemoving:Connect(function(player)
		local weapons = player.Weapons
		local coins = player.leaderstats.Coins 
		local PlayerUserId = "player_"..player.UserId
		local data = {}
		local dataweapons = weapons:GetChildren()
		data[weapons] = dataweapons
		data[coins] = coins.Value
		for i, v in pairs(data) do
			print(v)
		end
		local success, err = pcall(function()
			ds:SetAsync(PlayerUserId, data)
		end)
		if success then
			print("data saved succesfully")
		end
end)

1 Like

If you’re testing in Solo, most likely the server is closing down before the data has a chance to actually save. Along with when the player leaves, you should also use game:BindToClose(func) and save data then too. The benefit here is that bound functions on that API will keep the server alive until the function is done (there might be a timeout at some point).

Another note: It might be better to save all your data in one key as a table of all your stats there, instead of a bunch of different keys.

1 Like

By the way, the only error in the output is ServerScriptService.LAS:24: attempt to index nil with ‘coins’ and I tried printing everything out from data, but data isnt a table(what I want it to be)

That’s because your initial data is going to be nil in the data store. You should check if the data is nil and use a default value in those cases.

Ill look at it, ill mark solution once it works, thx

Oh and by the way, do I have to rename the datastore once the changes work, because nill will be the only data there?

I dont really know how to use bindtoclose but is this close?

local Leaving = function(player)
	local weapons = player.Weapons
	local coins = player.leaderstats.Coins 
	local PlayerUserId = "player_"..player.UserId
	local data = {}
	local dataweapons = weapons:GetChildren()
	data[weapons] = dataweapons
	data[coins] = coins.Value
	for i, v in pairs(data) do
		print(v)
	end
	local success, err = pcall(function()
		ds:SetAsync(PlayerUserId, data)
	end)
	if success then
		print("data saved succesfully")
	end
end

game.Players.PlayerRemoving:Connect(function(player)
	game:BindToClose(Leaving(player))
end)

Almost, just a bit in reverse:

game:BindToClose(function()
   for _,player in ipairs(game.Players:GetPlayers()) do
      -- Save data for `player`
   end
end)

Any closer?

game.Players.PlayerRemoving:Connect(function(player)
	game:BindToClose(function()
		for i, v in ipairs(game.Players:GetChildren()) do
			local weapons = player.Weapons
			local coins = player.leaderstats.Coins 
			local PlayerUserId = "player_"..player.UserId
			local data = {}
			local dataweapons = weapons:GetChildren()
			data[weapons] = dataweapons
			data[coins] = coins.Value
			for i, v in pairs(data) do
				print(v)
			end
			local success, err = pcall(function()
				ds:SetAsync(PlayerUserId, data)
			end)
			if success then
				print("data saved succesfully")
			end
		end
	end)
end)

I thought I had it, but data still is nill

game:BindToClose(function()
	for i, v in ipairs(game.Players:GetChildren()) do
		local weapons = v.Weapons
		local coins = v.leaderstats.Coins 
		local PlayerUserId = "player_"..v.UserId
		local data = {}
		local dataweapons = weapons:GetChildren()
		data[weapons] = dataweapons
		data[coins] = coins.Value
		for i, v in pairs(data) do
			print(v)
		end
		local success, err = pcall(function()
			ds:SetAsync(PlayerUserId, data)
		end)
		if success then
			print("data saved succesfully")
		end
	end
end)

The data isnt saving, maybe it just cant in time, or maybe im missing something important, well at the least I have found that its not that the data isnt able to go into the datastore, its that the data doesent save at all

its working now, thanks man. My issue was that I didnt know what a datastore could contain, but now I do.