Need help with make script simpler

Hello, the lost people,

I’m trying to make an item that you can pick it up after clicking it using clickdetector. But that is not the thing that I want to make it simpler.

See the script right here? I want to know how can I make it simpler and shorter. So, please help

You can make a folder only for keys and put:

local itemNames = (game.ServerStorage.itemName:GetChildren()) do
or ReplicatedStorage

Here is something you can use. It doesn’t really make it simpler, it just means that if you add more names to the itemNames array, you don’t have to recode it :slight_smile:

local itemNames = {
-- Put your names in here
}

local function onPickup(object, target)
   local itemFound = nil
   for index = 1, #itemNames do
      if target[itemNames[index]] then
         itemFound = target[itemNames[index]]
      end
   end
   if itemFound then
      local list = itemFound
      ...
end

Personally, I would use the script I just gave you because it means that if I change any values in the Array, I don’t have to re-program anything :slight_smile:

1 Like

If the target does not have an object with the specified name, it will error, FindFirstChild is required there

Also I dont know why you’re using a numerical for loop in this case when using pairs or ipairs is sufficient for the usecase

Think this would be a better way of doing what you had done

local itemNames = {
	-- Put your names in here
}

local function onPickup(object, target)
	local list
	for _, name in ipairs(itemNames) do
		list = target:FindFirstChild(name)
		if not list then
			continue
		end
		break
	end
	if list then
		...
end

let me try. I hope it work well