Attempted to index nil with number in a table

Hello developers,
I am trying to make a filter system for my game, and one of the features is searching up the duck you would like to know more about. This is the process/code leading to the error:

Client to Server, Server to Client (Same Remote-event)

print(makeInvisible[1], makeInvisible[2]) -- Error is here --------------------------------

Localscript:

local Alphabet = {
	"a",
	"b",
	"c",
	"d",
	"e",
	"f",
	"g",
	"h",
	"i",
	"j",
	"k",
	"l",
	"m",
	"n",
	"o",
	"p",
	"q",
	"r",
	"s",
	"t",
	"u",
	"v",
	"w",
	"x",
	"y",
	"z",
}
local Remote = script.Parent:FindFirstChild("FilterTextEvent")

script.Parent:GetPropertyChangedSignal("Text"):Connect(function()
	local OGText = script.Parent.Text
	local Text = string.lower(OGText)
	
	for _, letter in pairs(Alphabet) do
		if Text == letter or string.find(Text, letter, tonumber(string.len(Text))) then -- If Text == Letter (a == a) or it finds the letter in the string (abcd == b)
			Remote:FireServer(Text, OGText)
			return
		elseif OGText == "" then
			Remote:FireServer()
			return
		end
	end
end)

Remote.OnClientEvent:Connect(function(Player, makeVisible, makeInvisible)
	local Frame = script.Parent.Parent.AllDucksScrolling
	print(makeVisible[1])
	print(makeInvisible[1], makeInvisible[2]) -- Error is here --------------------------------
	
	for i = 1, #makeVisible do
		Frame:FindFirstChild(makeVisible[i]).Visible = true
	end
	
	for y = 1, #makeInvisible do
		Frame:FindFirstChild(makeInvisible[y]).Visible = false
	end
end)

Script:

local Remote = script.Parent:FindFirstChild("FilterTextEvent")
local makeVisible = {}
local makeInvisible = {}

Remote.OnServerEvent:Connect(function(Player, Text, OGText)
	local Frame = script.Parent.Parent.AllDucksScrolling
	
	if Text == nil then
		for _,v in ipairs(Frame:GetChildren()) do
			if v:IsA("ImageButton") then
				--v.Visible = true
			end
		end
		return -- After the loop is finished, we return so we don't run the loop under us.
	end
	
	table.clear(makeVisible)
	table.clear(makeInvisible)
	
	for _,v in ipairs(Frame:GetChildren()) do
		if v:IsA("ImageButton") then
			local Name = string.lower(v.Name)
			
			if string.sub(Name, 1, tonumber(string.len(Text))) == Text then -- If the name == the text using the text length then
				print(v.Name.." hi")
				table.insert(makeVisible, v.Name) -- Since we are making it visible on the client, we have to continue to make it visible on the client or weird things will happen
			else
				table.insert(makeInvisible, v.Name)
			end
		end
	end
	
	Remote:FireClient(Player, makeVisible, makeInvisible)
end)

I have looked at a-lot of other forum sites and devforum, but nothing helps me/matches the description of my problem.

I think your problem is when your server script fires the remote event, it will only send makeVisible and makeInvisible. The parameter Player is not sent in a remote event fired by server so your local script gets the argument player as makeVisible and makeVisible as makeInvisible.

As you can see, makeInvisible becomes nil, making your code error because you’re indexing a nil value.
Just delete the player argument in the local script and everything should work.

In this part:

2 Likes

Yeah, once I published this I figured out what I did wrong, I was hoping nobody was going to notice.

Thank you though!

1 Like