Checking if value is in table

Hi there. I’m trying to make so that when you touch any part within a model with a tool, it will check if the tool name equals to any value inside of my table. I have searched around for surely 1 hour but haven’t found anything that works with this (all touched events works perfectly, by the way)

local Tools = {"Sword", "Gun"}

for i,v in pairs(script.Parent.Model:GetChildren()) do
	v.Touched:Connect(function(Touch)
			if Tools[Touch.Parent] then
			print("Works")
		end
	end)
end

If you could tell me how I can fix this, it’d be very appreciated! If anything is unclear, please tell me below.

4 Likes

You can use table.find, the same way as table.insert
if table.find(Tools, touch) do

8 Likes

Still does not work. Does not output anything either.

Make sure to use the Name property, and not the actual Instance itself.

1 Like

Just tested this and this should work (you can make this a child of the model this applies to, or just specify where your model is located in the code):

local Model = script.Parent

local Tools = {"Sword", "Gun"}

for i, v in pairs (Model:GetChildren()) do
	v.Touched:Connect(function(Touch)
		if Touch.Parent:IsA("Tool") then
			local Tool = Touch.Parent
			
			for x, c in pairs (Tools) do
				if c == Tool.Name then
					print("Works")
				end
			end
		else
			return
		end
	end)
end

Your error just comes from the fact that you can’t access a table value by doing Table[value]. You have to specify the index if you’re doing it that way. You could have checked like

if Tools[1] == Touch.Parent.Name or Tools[2] == Touch.Parent.Name --// and so on and on, but this gets long...

In the snippet I just sent you what is basically happening is that we specify the model, create a table of its parts, parse through each part and tag an event onto it (this is exactly what you were doing) and then once the event is triggered check to see if whatever touched it is part of a tool (this could potentially lead to an error if the part isn’t a direct child of a Tool like if your tool has models or folders inside of it). If the part is indeed a child of a tool, we parse through the table of Tool names i.e. Tools that you defined earlier in the same way that we parsed through the parts of the model. Then we just check to see if the Name of the tool is equal to the strings in the table!

Let me know if that makes sense or if it’s not working as you expect!

8 Likes

I would recommend using table.find instead of looping through the table.

if table.find(Tools,Tool.Name) then
    -- Code
end
29 Likes

Suggesting loops where not necessary is bad advice. Don’t misuse or abuse loops, use the proper ways of accomplishing this. Either you have the option of converting this to a dictionary or using table.find, but absolutely do not use loops here.

2 Likes

You’ve got the right idea, but you’ve done it wrong: “do”, pal.

Use table.find function and you also need the .Name to provide it is infact a string.

table.find(Tools, Touch.Parent.Name)

table.find only works for an array though.

If you have a dictionary, like {
[“A”] = x,
[“B”] = y
}
you should use “for key, value do” loop.

I learn this the hard way :slight_smile: