Table.insert not working

As you may figure from reading this server script, it is a datastore. On line 134, the table.insert function refuses to insert the given value. Both of the values (filex and the value being inserted) are not equal to nil. When printed out on the lines below line 134, they all print nil except for the i,v in pairs look. I don’t know why this would be happening because when I printed table.find(filex, v) in the for i,v in pairs loop, it printed out nil as well

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("knight3")
local swordsave = {}

local function saveData(player)
	
	local tableToSave = {
		player.leaderstats.Skulls.Value;
		player.leaderstats.Tix.Value;
		swordsave[player.UserId]
	}
	if swordsave[player.UserId] ~= nil then
		for i,v in pairs(swordsave[player.UserId]) do
			print(player.Name, v)
		end
	end
	local success, err = pcall(function()
		print("saved")
		dataStore:SetAsync(player.UserId, tableToSave)
	end)
end

game.Players.PlayerAdded:Connect(function(player)
	table.insert(swordsave, player.UserId)
	swordsave[player.UserId] = {}
	print(typeof(swordsave[player.UserId]))
	
	local playertools = Instance.new("Folder", game.ServerStorage.Tool)
	playertools.Name = player.Name

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

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

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

	local data

	local success, err = pcall(function()
		
		data = dataStore:GetAsync(player.UserId)

	end)

	if success and data then
		Skulls.Value = data[1]
		Tix.Value = data[2]
		local redo = data[3]
		if redo ~= nil then
			print("ok")
			for i,v in pairs(redo) do
				print("ok2")
				table.insert(swordsave[player.UserId], v)
			end
		end
		if swordsave[player.UserId] ~= nil then
			for i,v in pairs(swordsave[player.UserId]) do
				print(player.Name, v)
			end
		end
	end
	print(typeof(swordsave[player.UserId]))
	local playertools = game.ServerStorage.Tool[player.Name]
	if swordsave[player.UserId] ~= nil then
		for i,v in pairs(swordsave[player.UserId]) do
			if typeof(v) == "string" then
				local str = string.split(v, " ")
				local sword = str[1]
				local file = str[2]
				game.ReplicatedStorage:WaitForChild("WeaponStorage"):WaitForChild(sword):Clone().Parent = game.ServerStorage:WaitForChild(player.Name):WaitForChild(file)
				game.ReplicatedStorage.DataStoreUsage.DTS:FireClient(player, swordsave, str)	
			end
		end
	end
end)

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

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local success, err  = pcall(function()
			saveData(player)
		end)
	end
end)

local db = false

game.ReplicatedStorage.DataStoreUsage.val.OnServerEvent:Connect(function(player, val, boolen)
	local uoe
	local filex = swordsave[player.UserId]
	print(filex)
	local don = false
	if boolen == true then
		print("Unequip not worko???")
		uoe = " Equipped"
		don = true
	elseif boolen == false then
		if don == false then
			print("Equip not worko???")
			uoe = " Unequipped"
		end
	end
	don = false
	if table.find(filex, tostring(val) .. " Unequipped") == nil and table.find(filex, tostring(val) .. " Equipped") == nil then
		print("Added new")
		uoe = " Equipped"
	end
	if filex ~= nil and val ~= nil and uoe ~= nil and boolen ~= nil then
		for i,v in pairs(filex) do
			local dont = false
			if table.find(filex, tostring(val) .. " Unequipped") ~= nil or filex[tostring(val) .. " Unequipped"] ~= nil then
				print("deleted")
				local found = table.find(filex, val .. " Unequipped")
				filex[found] = nil
				dont = true
			end
			if table.find(filex, tostring(val) .. " Equipped") ~= nil or filex[tostring(val) .. " Equipped"] ~= nil then
				if dont == false then
					print("deleted")
					local found = table.find(filex, val .. " Equipped")
					filex[found] = nil
				end
			end
		end
		table.insert(filex, tostring(val) .. uoe) ---------- LINE 134, ISSUE
		print("insertted ", filex, tostring(val) .. uoe)
		print(table.find(filex, tostring(val) .. uoe))
		for i,v in pairs(filex) do
			print(v)
		end
	end
end)

could you please tell us where line 134 is as i dont really feel like counting down 134 lines :wink:

Line 134 is 7 lines from the bottom of the code. It’s marked with a comment (although I copied the code into an empty script to find the line :stuck_out_tongue:).

1 Like

if you scroll down to the bottom, you will find a comment that says “LINE 134, ISSUE”