How to Use local to reference multiple strings?

Im trying to make a script that takes tools that have the names Listed in Tool_Names but the issue is no tools get taken. Nothing comes up in output so I think the issue is with the local Tool_Names or the way I used the local Tool_Names in the script

local Tool_Names = {
	"Headlock","Rush","BodySlam"
}
for _, tools in pairs(plr.Backpack:GetChildren()) do
	if tools.Name == Tool_Names then
		tools.Parent = plr.TS
        --// plr.TS is a folder inside the player to store tools
	end
end

You’d want to use table.find since you’re checking if the name of the tool is in the Tool_Names list.

local Tool_Names = {
	"Headlock","Rush","BodySlam"
}
for _, tools in pairs(plr.Backpack:GetChildren()) do
	if table.find(Tool_Names, tools.Name) then -- if table.find doesn't find it, it will return nil and the check doesn't pass
		tools.Parent = plr.TS
	end
end

The way you were doing it before was comparing tools.Name to the Tools_Name list:

-- you were doing:
if <string> == <table> then ...
2 Likes

This doesn’t work with arrays:

local t = { "Hello" }
local v = t["Hello"]

print(v) --> prints 'nil'

It’s only applicable to dictionaries:

local t = { Hello = "World" }
local v = t["Hello"]

print(v) --> prints 'World' 
1 Like

Oh I never knew that thanks for your help!

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