How do I select multiple objects of the same type and do something with them?

I have a lot of tools in ReplicatedStorage, and want to clone and put them in the players backpack, but I don’t want to copy all of the tools into the player’s backpack. I tried putting them in a folder using GetChildren() but when trying to clone a GetChildren variable the output says:

Workspace.Plot1Build.Script:4: attempt to call a nil value

Any way to copy multiple tools without having to write variables for them individually?

2 Likes

for loop with GetChildren() and clone. Share you code here so we can help you fix that issue instead.

ok I made a for loop, here it is:

local FixedTools = game.ReplicatedStorage.FixedTools:GetChildren()	
script.Parent.Touched:Connect(function(hit, player)
	if hit.Parent:FindFirstChild("Humanoid") then
		for _, child in pairs(FixedTools) do
			local FixedClone = FixedTools:Clone()
			FixedClone.Parent = player.Backpack
		end
	end
end)
1 Like

So what exactly is the issue? Doesn’t it work fine.

If you want to select objects of the same type, assuming a tool you just need to see what you’re looping over is a tool.

for_, child in pairs(FixedTools) do
   if child:IsA("Tool") then
1 Like

I changed the code a little, now I get this error:Untitled3
new code:

local FixedTools = game.ReplicatedStorage.FixedTools:GetChildren()	
script.Parent.Touched:Connect(function(hit, player)
	if hit.Parent:FindFirstChild("Humanoid") then
		for _, child in pairs(FixedTools) do
			if child:IsA("Tool") then
				local childclone = child:Clone()
				childclone.Parent = player.Backpack
			end
		end
	end
end)

You havent define what the backpack is.

There’s a possibility that hit.Parent is not getting the player or something along the lines of that
You can get the player using :GetPlayerFromCharacter

if hit.Parent:FindFirstChild("Humanoid") then
   local player = game.Players:GetPlayerFromCharacter(hit.Parent)
2 Likes

I tried it and it now works, thank you.

1 Like