Attempt to call a nil value - table.insert()

this is for a save queue. when the value changes the player is supposed to get added to a table in which the players in the table will get saved

-- add to save queue if changed
	for i, v in ipairs(player.Settings:GetDescendants()) do
		v.Changed:Connect(function()
			table.insert(players_to_save,player) -- line 63, as mentioned in da output
			print(player.DisplayName.." settings have changed and are now in the save queue")
		end)
	end

the output:
Screen Shot 2023-09-04 at 2.11.13 AM

thank you so much for any help :heart:

Did you double-check you have players_to_save as a table? The error itself means it didn’t find anything for player or players_to_save I believe.

1 Like

this is the table

local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("SettingsDataStore")
local players = game:GetService("Players")
local players_to_save = {} -- table
local auto_save_interval = 120 -- every 120 seconds
local error = game.ReplicatedStorage.Extras.Message

can i not do it this way?

Yeah, that’s fine, don’t worry. Are you sure it’s in the exact same code as the function where you put players into a save queue?

1 Like

im not sure what you mean, here’s the whole script:

local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("SettingsDataStore")
local players = game:GetService("Players")
local players_to_save = {}
local auto_save_interval = 120 -- every 120 seconds
local error = game.ReplicatedStorage.Extras.Message

local function save(player)
	-- if loaded correctly
	if player.LoadedCorrectly[script.Name].Value == true then

		local settings = player:WaitForChild("Settings")
		local key = "Settings_"..player.UserId

		local table = {}
		for i,setting in ipairs(settings:GetChildren()) do
			table[setting.Name] = setting.Value
		end

		local success, errormessage = pcall(function()
			ds:SetAsync(key, table)
		end)

		if success then
			-- yay :]
		else
			print(script.Name.." save failed")
			warn(errormessage)
			local error = error:Clone()
			error.Parent = player.PlayerGui.PopUps
			error.Message.Text = ("Your "..script.Name.." failed to save")
		end
	end
end

players.PlayerAdded:Connect(function(player)
	local key = "Settings_"..player.UserId
	local table = {}
	local success, errormessage = pcall(function()
		table = ds:GetAsync(key) or {}
	end)
	
	-- if success, set loaded correctly value to true
	if success then
		player:WaitForChild("LoadedCorrectly"):WaitForChild(script.Name).Value = true
	else
		print(script.Name.." failed to load")
		warn(errormessage)
		local error = error:Clone()
		error.Parent = player.PlayerGui.PopUps
		error.Message.Text = ("Your "..script.Name.." failed to load")
	end
	
	-- make values in folder
	for field, value in pairs(table) do
		local setting_value = player:WaitForChild("Settings"):WaitForChild(field)
		setting_value.Value = value
	end
	
	-- add to save queue if changed
	for i, v in ipairs(player.Settings:GetDescendants()) do
		v.Changed:Connect(function()
			table.insert(players_to_save,player)
			print(player.DisplayName.." settings have changed and are now in the save queue")
		end)
	end

end)

-- when leaving, if needed
players.PlayerRemoving:Connect(function(player)
	if table.find(players_to_save,player) then
		save(player)
		table.remove(players_to_save,table.find(players_to_save,player))
	end
end)

-- autosave, if needed
while task.wait(auto_save_interval) do
	for i, player in ipairs(players_to_save) do
		save(player)
		table.remove(players_to_save,table.find(players_to_save,player))
	end
end

game:BindToClose(function()
	if not game:GetService("RunService"):IsStudio() and #players:GetPlayers() > 1 then
		for i, player in ipairs(players:GetPlayers()) do
			save(player)
		end
	end
end)
1 Like

change the name of this table and it should work

2 Likes

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