How do i check all table items without for loops?

Hello, I am working on item dropping when player dies

local character = script.Parent
local player = game.Players:GetPlayerFromCharacter(character)
local humanoid = character:WaitForChild("Humanoid")

local restrictedItems = {
	game.ReplicatedStorage.Tools.GoldStunStick;
}

humanoid.Died:Connect(function()
	for _,i in pairs(character:GetChildren()) do
		if i:IsA("Tool") then
			i.Parent = workspace
			if not i:FindFirstChild("PickScript") then
				local s = game.ReplicatedStorage.PickUp.PickScript:Clone()
				s.Parent = i
				s.Disabled = false
			end
		end
	end
	for _,i in pairs(player.Backpack:GetChildren()) do
		if i:IsA("Tool") then
			i.Parent = workspace
			if not i:FindFirstChild("PickScript") then
				local s = game.ReplicatedStorage.PickUp.PickScript:Clone()
				s.Parent = i
				s.Disabled = false
			end
		end
	end
end)

And as you see restrictedItems table, i want to check ALL of items in this table and in for loops check if item is not any of items in restrictedItems table. Is it possible?

Thanks!

1 Like

You can use table.find for your use I believe

local items = {
    "Sword",
    "Katana",
    "Baked Beans"
}

if table.find(items, "Sword") then
   print("Found sword")
end

You can put not before the tale to find to do something if the item was not found i nthe table. You can just include in your loops if table.find(restricteditems, youritem) then continue end to ignore restricted items

4 Likes

yes it is just scan the backpack and then clone the tool on the player when they die

how is that related to the current topic i’m just helping you with your problem and how to expand upon it

do you need any help on this problem or not

The post has been already solved a day ago

If you care about performance, you’d be better off indexing into a dictionary instead. By simply storing them like this =

local items = {

["Sword"] = SwordInstance;
["Katana"] = KatanaInstance;

};

From what I know, table.find loops through the array, meaning it can impact performance with large tables.

I mean the performance drop it has it marginal at best depending on the size of the table. Even if @OP has 20 tool names to go through, there’s still no real difference in speed between table indexing and dictionary indexing.

Yes there is a difference but it’s a difference of nano/micro seconds, something no one is going to notice.

Only time it’s going to have an issue is if there’s thousands of entires in that table, which sounds unlikely given the circumstances