Issue with if statement inside a loop in a function with tables

Hi, I have this piece of code that is supposed to output “1” if a value from a table is identical from the input of my function, but I end up getting “0” as a output instead.
I tried changing the if statement itself, restarting studio, re-writing it on a different script, looked up for any solutions, and I haven’t found anything regarding this issue.

This is the current script that I extracted from my project:

-- Put this in a server script

local bonusContraptions = {}

function GetCustomContraption(Name)
	local returned = 0 --This stays at 0 unless both values are identical
	for i, v in pairs(bonusContraptions) do
		warn(v.." "..Name) --output 1
		if Name == v.Name then
			returned = 1
			warn("FOUND EM") --output 2
			break
		else
			warn("NOPE") --output 3
		end
	end
	return returned
end

table.insert(bonusContraptions, "Object") --Adding a string into the table

print(GetCustomContraption("Object")) --Checking for that string, output 4

--[[

Expected output:

Output 1: "Object Object"
Output 2: "FOUND EM"
Output 4: "1"

Obtained output:

Output 1: "Object Object"
Output 3: "NOPE"
Output 4: "1"

note that what I got should happens ONLY if both values aren't identical.

]]

If you have any questions, i’ll be here to reply.
Thanks in advance.

it’s probably with the “v.Name”, i don’t see any other values besides a string that have the property Name. you’re basically comparing if Name is nil.

Ight thank you very much for the help!

1 Like

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