LocalScript can't find a value in a table, even though it's there?

Hi, I’ve been having this EXTREMELY frustrating issue I try and index a key from a table, and then it just says “Nope, not there” and prints out a nil

Here’s my code:

local producables = {
	"Factory",
	"Barracks"
}

local chosen = nil
local folder = nil

for _,v in pairs(selection:GetChildren()) do
	local val = v.Value
	if table.find(producables, val) then -- is it a producable? (can it produce stuff)
		print('found')
		chosen = v.Value
		folder = game.ReplicatedStorage.Units[val]
		break
	end
end

print(folder)

Note: The folder of “Selection” includes stringValues of the selected object. Don’t ask why.

are you sure the string values are the exact same as the strings in the table?

Ah yes, tables my enemies

You should probably check to make sure that the Objects you’re getting through inside the Instance are valid Value objects

Try this?

local producables = {
	"Factory",
	"Barracks"
}

local chosen = nil
local folder = nil

for _,v in pairs(selection:GetChildren()) do
	local val = v.Value
    print("String's Name: "..v)
    print("String's Value: "..val)
	if v:IsA("StringValue") and table.find(producables, val) then -- is it a producable? (can it produce stuff)
		print('found')
		chosen = v.Value
		folder = game.ReplicatedStorage.Units[val]
		break
	end
end

print(folder)
1 Like

Yes. 100%. 100/100. Absolutely sure. I literally copy pasted the name right into the table.

Nope, still prints nil. For a second I thought I accidentally made it an ObjectValue, but apparently not!

UPDATE: I’m still wrong (again). The WHOLE time I was using an object value and not a string value. Thanks for helping though

2 Likes

Ah yes, facepalming yourself 100 times you’re fine at least you glad it fixed :sweat_smile:

1 Like