Table.insert function not seeming to work

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 loop (when I print v it says what is expected). 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
		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)
1 Like

filex seems to be a number instead of a table.

i can confirm that it is infact a table (specifically a dictionary)

you can’t use table.insert with dictionaries, you’d have to write into the dictionary

local keys = {}

keys["Code1"] = 1234
keys["Code2"] = 4321

etc

if you looked at the script, you would see that i did this right after the PlayerAdded event

table.insert(swordsave, player.UserId)
swordsave[player.UserId] = {}

because table.insert isn’t setting the index as the player userid. it sets it to the length of the table + 1. you are trying to access the table as if it was a dictionary.

1 Like

ok let me rephrase what I said earlier: the table.insert function does work, it just doesnt insert the right value.

i know what you’re trying to do but the way you’re trying to access is it wrong.

you created a table in the dictionary of swordsave with the index of the player’s userid. you cannot access this table by saying table.insert(swordsave, player.UserId) because you’re turning the dictionary into a mixed table with stored positional data and indexes.

to access the table you created you’d have to write

if not swordsave[player.UserId] then
   swordsave[player.UserId] = {}
end

table.insert(swordsave[player.UserId], "DataHere")
1 Like

Okay let me explain this in detail. In the code below, the if statement is getting passed.

	if table.find(filex, tostring(val) .. " Unequipped") == nil and table.find(filex, tostring(val) .. " Equipped") == nil then
		print("Added new")
		uoe = " Equipped"
	end

Now, when the table.insert function is called it will insert the “uoe” value above. When I print out what hade been inserted, it prints nil when all values are not equal to nil. But for no reason when I do the for i,v in pairs loop, it prints what I inserted. This repeats forever. There is more details but I would have to make a paragraph to explain.

When you loop through the filex table, print the index i then tell me what prints.

sorry for the long wait

it prints 1, 2, 3, 4, 5, etc. for every time i click on the button. a number is added every time.

Sorry for the long wait as well.

Can you try this for loop

for i,v in pairs(filex) do
	if table.find(filex, tostring(val) .. " Unequipped") then
		print("deleted")
		local found = table.find(filex, tostring(val) .. " Unequipped")
		if found then
			table.remove(filex,found)
		end
	elseif table.find(filex, tostring(val) .. " Equipped") then
		print("deleted")
		local found = table.find(filex, tostring(val) .. " Equipped")
		if found then
			table.remove(filex,found)
		end
	end
end

oh my gosh dud thank you so much this is actually working rn i cannot express how much i appreciate you rn dude

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