Table returns nil, despite that a value is in there (From i, v in pairs)

Hello, so I’ve been scripting a ban and unban system. And I’ve been using a table called “manual” to store names of people who’ve been banned. Although there is the table, it’s still consider the table as nil. Can anyone help me with this?

It considers the table value as nil, although I’ve already created a variable and marked it as a table.

Here is the script

local AdminList = require(script.Parent.Settings.AdminList)
local CommandName = require(script.Parent.Settings.CommandName)
local Other = require(script.Parent.Settings.CommandName)

local DS = game:GetService("DataStoreService")
local Bans = DS:GetDataStore("Key2")

if Other.enabled == false then
	script.Disabled = true
end

local manual = {}

game.Players.PlayerAdded:Connect(function(plr)
	for i, v in pairs(AdminList.Adminlist) do
		if v == plr.Name then
			print("[HADES]: Welcome, administrator.")
		end
	end
	local data
	local success, err = pcall(function()
		data = Bans:GetAsync("Key2")
	end)
	
	
	if success then
		manual = data
		print("[HADES]: Banlist has been updated.")
	else
		print("[HADES]: Banlist has failed to update.")
		warn("[HADES]: " ..err)
	end
	
	for i, v in pairs(manual) do
		if v == plr.Name then
			plr:Kick("You are banned.")
		end
	end

	
	plr.Chatted:Connect(function(msg)
		local mes = msg:lower()
		local splitted = mes:split(" ")
		
		if splitted[1] == "!kick" then
			print(msg)
			for i, v in pairs(AdminList.Adminlist) do
				if v == plr.Name then
					local target = splitted[2]
					for i, v in pairs(game.Players:GetPlayers()) do
						for i = 1, v.Name do
							if splitted[2] == string.sub(v.Name, 1, i) then
								v:Kick("By admin.")
							end
						end
					end
				end
			end
		end
		
		if splitted[1] == "!ban" then
			for i, v in pairs(AdminList.Adminlist) do
				if v == plr.Name then
					local target = splitted[2]
					for i, v in pairs(game.Players:GetPlayers()) do
						for i = 1, v.Name do
							if splitted[2] == string.sub(v.Name, 1, i) then
								v:Kick("Banned from the game.")
								table.insert(manual, #manual+1, tostring(v.Name))
								local success, err = pcall(function()
									Bans:SetAsync("Key2", manual)
								end)
								
								if success then
									print("[HADES]: Ban registered.")
								else
									warn("[HADES]: Ban log failed.")
									warn("[HADES]: " ..err)
								end
								for i, v in pairs(manual) do
									print(v)
								end
							end
						end
					end
				end
			end
		end
		if splitted[1] == "!unban" then
			for i, v in pairs(AdminList.Adminlist) do
				if v == plr.Name then
					local target = splitted[2]
					for i, v in pairs(game.Players:GetPlayers()) do
						for i = 1, v.Name do
							if splitted[2] == string.sub(v.Name, 1, i) then
								for index, val in pairs(manual) do
									if val == v.Name then
										table.remove(manual, index)
										local success, err = pcall(function()
											Bans:SetAsync("Key2", manual)
										end)

										if success then
											print("[HADES]: Unban registered.")
										else
											warn("[HADES]: Unban failed.")
											warn("[HADES]: " ..err)
										end
									end
								end
							end
						end
					end
				end
			end
		end
	end)
end)```

You set the value of manual to ‘data’ at one point, which was Bans:GetAsync(“Key2”). The original value for manual - {} is overwritten. This is probably not a table anymore.

Idk so don’t quote me.

No, I don’t think that’s the thing that rewritten the table value. As it saves the table, not overwrite it. The only thing that might be overwritten the table is this line:

table.insert(manual, #manual+1, v.Name)

Also getting the data variable is the collected data from the datastore, which is the updated banlist.

If you want to see if the player’s UserId is in the table then you could use:

if table.find(table_here, plr.UserId) then
-- Insert code here
end

I’m using username as for the data stored in the table. Also it’ll just return nil as the output.

Your data store “Key2” likely has no data in it so it will return nil and then you’re setting the table to be that data which will make it nil.

I’ve switched back to my old DataStore “Key1”, it worked. But when I switched back to Key2, Key3 and so on, why is it decided not to work? Could you explain it for me please?

Fixed it, I just put in a few extra lines in the case if data is nil. I’ll mark your answer as the solution.