Is there a faster more efficient way to script a accessory removal script? I'm using else if and it's too slow

The code below will remove the player accessory which works but It’s not as fast as I want it to be. When the player resets you can see each accessory being removed within a second, you’ll see a flicker. Is there a more efficient way to write this? I want the code to execute all at once.

local function onPlayerAdded(player)
	player.CharacterAppearanceLoaded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		for i, v in pairs(char:GetChildren()) do
			if v:IsA("Accessory") then
				if v.Handle:FindFirstChild("FaceCenterAttachment") then
					v:Destroy()
				elseif v.Handle:FindFirstChild("LocalCollarAttachment") then
					v:Destroy()
				elseif v.Handle:FindFirstChild("FaceFrontAttachment") then
					v:Destroy()
				elseif v.Handle:FindFirstChild("BodyFrontAttachment") then
					v:Destroy()
				elseif v.Handle:FindFirstChild("WaistCenterAttachment") then
					v:Destroy()
				elseif v.Handle:FindFirstChild("NeckAttachment") then
					v:Destroy()
				elseif v.Handle:FindFirstChild("BodyBackAttachment") then
					v:Destroy()
				end
			end
		end
	end)
end
game.Players.PlayerAdded:Connect(onPlayerAdded)

You could just destroy the accessory. So you go with like “v:Destroy” instead of checking for attachments.

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(plr)
	plr.CharacterAppearanceLoaded:Connect(function(char)
		for i, v in pairs(char:GetChildren()) do
			if v:IsA("Accessory") then
				if v:FindFirstChild("Handle") then
					local handle = v:FindFirstChild("Handle")
					for i, val in pairs(handle:GetDescendants()) do
						val:Destroy()
					end
				end
			end
		end
	end)
end)

I’m assuming you want the handle to not be deleted. If you want the handle to be deleted as well then use the following instead:

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(plr)
	plr.CharacterAppearanceLoaded:Connect(function(char)
		for i, v in pairs(char:GetChildren()) do
			if v:IsA("Accessory") then
				if v:FindFirstChild("Handle") then
					v:FindFirstChild("Handle"):Destroy()
				end
			end
		end
	end)
end)

Is there a reason you’re removing accesories?

If you don’t want players to have an appearance, simply disable LoadCharacterAppearance from the StarterPlayer object in your explorer.

Thanks for the quick reply but, I want all accessories except and hat and hair removed, the script removes everything. Also, I can still see the accessory being removed from the character. My game is an obby so there would be a lot of respawning in-game and the flicker doesn’t look good.

I posted 2 scripts, one of them deletes the handle of the accessory entirely, the other just deletes all of the handle’s attachments.

local attachments = {"FaceCenterAttachment", "LocalCollarAttachment", "FaceFrontAttachment", "BodyFrontAttachment", "WaistCenterAttachment", "NeckAttachment", "BodyBackAttachment"}
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(plr)
	plr.CharacterAppearanceLoaded:Connect(function(char)
		for i, v in pairs(char:GetChildren()) do
			if v:IsA("Accessory") then
				for i, val in pairs(v:GetDescendants()) do
					if table.find(attachments, val.Name) then
						val:FindFirstAncestorOfClass("Accessory"):Destroy()
					end
				end
			end
		end
	end)
end)

How about this?

1 Like

Works perfectly thank you so much!!