"For i, v in pairs" not working as expected

So, I have a website gui that checks if the search bar text is the same as a value in a table.

But, when I click the “Go” button on my UI, it prints “Invalid URL”, however, it’s not “invalid”. I know this because when I print the table, it has the URL.

Code:

local pages = script.Parent.Parent.Parent.Bg.Pages
local search = script.Parent.Parent.Parent.Bg.Bar.Search.SearchBox
local enter = script.Parent.Parent.Parent.Bg.Bar.Search.Enter
local validURL = {}



for i, v in pairs(pages:GetChildren()) do -- Adds each page to the table
	table.insert(validURL,v.URL.Value)
	wait(2)
	print(validURL)
end


enter.MouseButton1Click:Connect(function()	
	for i, v in pairs(validURL) do
		if search.Text == v then
			print("Valid URL")
		else
			print("Invalid URL") -- URL is in the table (as seen above)
		end
	end
end)

Output (table):
image

Please help!

EDIT: it also prints “Invalid URL” if I change it to "if v == search.Text then"

you could use table.find() insteed of search.Text == v

Tried it, same result

character stuff

try
print(“comparing:”, search.Text, v)
just to get a better understanding of what is going on.

If you see two that get compared that look the same, they might be different datatypes.
print(“comparing:”, typeof(search.Text), typeof(v))

You are using a server script, you need to use a localscript for this to work. The server cannot see changes made by the client.

2 Likes

Would RemoteEvents work?

This text will be blurred

I mean you can just change it to localscript unless you want to modify anything on the server.

1 Like

if search.Text == v then is the same as if v == search.Text then, the order of operands in a single comparison operation does not matter.

Try printing ‘search.Text’ and ‘v’ when they’re being compared, you may notice an unexpected difference between the two (unintended whitespaces, mispellings etc.).

image

That is the typeof output. How about the plain search.Text and v output?

1 Like

Oh my god, you freaking genius!

Thank you so much!