Just wanna delete all accesories in character

attempted code

local char = player.Character or player.CharacterAdded:Wait() and player.CharacterAppearanceLoaded:Wait()


	local charchildren = char:GetChildren()

while wait(1) do 
	print(charchildren)
end


for i, v in pairs(charchildren) do
	

	if v:IsA("Accessory") then
		local accesory = v
		accesory:Destroy()
	end
	end

I have tried looping through player to see if its even being processed, but it isnt for some reason, this is what loop look’s like

image
, also get this weird little error, but i searched it up and apparently it doesnt affect anything

Your random loop is causing the problem.

Code:

local char = player.Character or player.CharacterAppearanceLoaded:Wait()

for i, v in char:GetDescendants() do
	if v:IsA("Accessory") then
		v:Destroy()
	end
end

formatted correctly and errors pointed out

-- wait for just characterAppearanceLoaded because we need to wait for accessories to load in
local char = player.Character or player.CharacterAdded:Wait() and player.CharacterAppearanceLoaded:Wait()

local charchildren = char:GetChildren()

--infinite loop; blocks the execution of the rest of the code after it
while wait(1) do 
	print(charchildren)
end

-- the variable "v" is the current child of the character
-- its not good practice to assign a new variable to "v" when you can just rename "v" to "accessory"
for i, v in pairs(charchildren) do
	if v:IsA("Accessory") then
		local accesory = v
		accesory:Destroy()
	end
end

fixed version

local char = player.Character or player.CharacterAppearanceLoaded:Wait()

local charChildren = char:GetChildren()
print(charChildren)

for i, accessory in pairs(charChildren) do
  if accessory:IsA("Acessory") then
    accessory:Destroy()
  end
end
1 Like

Humanoid:RemoveAccesories()

easy

Hmmm still doesnt work , thank you for the tips though ill keep that in mind nexttime : )

function doesnt work, the print doesnt show accessories too, weird

:smiley: placing it inside a loop seem to work ``` local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAppearanceLoaded:Wait()

while wait() do
for i, v in char:GetDescendants() do
if v:IsA(“Accessory”) then
v:Destroy()
end
end

end

The reason is obvious.

You use a loop:

while wait(1) do 
     --nv
end

Then you create another loop that is supposed to remove accessories, but it won’t be called because the “while true do” loop keeps looping and stops in one place.

I wouldn’t prefer you to use loops within loops. Just use the function:

coroutine.wrap

That is:

local char = player.Character or player.CharacterAdded:Wait() and player.CharacterAppearanceLoaded:Wait()

local charchildren = char:GetChildren()

local WhileLoop = coroutine.wrap(function()
	while wait(1) do 
		print(charchildren)
	end
end)

local DeleteAccesories = coroutine.wrap(function()
	for i, v in pairs(charchildren) do
		if v:IsA("Accessory") then
			local accesory = v
			accesory:Destroy()
		end
	end
end)

WhileLoop() -- Start Loop which print "charchildren"
DeleteAccesories() -- Start "For" loop which delete accesories

Using the “coroutine.wrap” function means that the script does not stop in one place and simply signs as

local FunctionName = coroutine.wrap(function()
      --Code
end)

FunctionName() --Activating function

If you’re using loop, coroutine.wrap will play until you don’t break it.

Ultimately, it turns out that both loops will be launched at the same time and the script will not get stuck in place. Now everything will work simultaneously, without lags or stopping the script.

You can pin my answer so others can learn about it too.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.