Problem with in pairs, and string.match

for i,v in pairs(game.ServerStorage.Clothing:GetChildren()) do
			print(string.match(v.Name, name))
			local Item = string.match(v.Name,name)
			spawn(function()
			if Item then
			
				if plr.Character:FindFirstChildWhichIsA("Pants") then
				plr.Character:FindFirstChildWhichIsA("Pants"):Destroy()
				end
				if plr.Character:FindFirstChildWhichIsA("Shirt") then
				plr.Character:FindFirstChildWhichIsA("Shirt"):Destroy()
				end
				v:Clone().Parent = plr.Character
				
			end
			end)	
			end

Hello! Basically, everything works, but it’s cloning 1 clothing (sometimes pants or sometimes shirt) but it doesn’t works, it’s an outfit which should be cloned, but it clones sometimes pants or sometimes shirt, I tried to add spawn(function() but still it clones 1.

Problem here is a logic error on your side. If you pay close attention, for every item it’ll check to see if you’ve got a Shirt and Pants in your character. As such, it’ll destroy them each time it goes through the list of items you’re trying to add.

This piece of code should work for your use case.

-- runs *before* adding clothing instances
if plr.Character:FindFirstChildWhichIsA("Pants") then
	plr.Character:FindFirstChildWhichIsA("Pants"):Destroy()
end
if plr.Character:FindFirstChildWhichIsA("Shirt") then
	plr.Character:FindFirstChildWhichIsA("Shirt"):Destroy()
end

for i,v in pairs(game.ServerStorage.Clothing:GetChildren()) do
	print(string.match(v.Name, name))
	local Item = string.match(v.Name,name)
	spawn(function()
		if Item then
			v:Clone().Parent = plr.Character
		end
	end)	
end
3 Likes