Problem With Deleting Certain Objects Of Specific Class In Model

Hello, I am trying to delete a character’s accessory items when they join the game, along with that I destroy all items that are character meshes and set body color.

This is the script I mention in this post:

As you can see I am using the same code to delete character meshes (which works) and character accessories (which doesn’t work yet its the same code?).

It finds and deletes all objects in the character’s model that are “CharacterMesh” but not all objects that are of the class “Accessory” (I checked if I spelled it right multiple times and for other mistakes).

When I run the script I do NOT get any errors, which is weird, since its not actually deleting any accessories. Usually it would say that the class doesn’t exist or some other error.

In game as you can see in my player model, accessories do exist in the model:
helpme_accessory

And every time I run the game all runs “fine” but my player still has accessories on:
helpme_character

Does anyone know what I am doing wrong and how to make the script work, thanks :slight_smile:

2 Likes

Try this.

game.Players.PlayerAdded:connect(function(player)
    local character = player.Character or player.CharacterAdded:Wait()
    for i,v in pairs (character:GetChildren()) do
        if v:IsA("Accessory") then
          v:Destroy()
        end
    end
end)
2 Likes

I tried it and it did not remove the accessories of my player and did not give any errors :confused:

I see, perhaps this?

workspace.ChildAdded:connect(function(char)
	for i,v in pairs (char:GetChildren()) do
		if v:IsA("Accessory") then
			v:Destroy()
		end
	end
end)

Well, I used the script that you provided above for character mesh also and it works perfectly fine for character mesh but not for accessories. So your script above was correct its just that for some reason its not removing player accessories.

Edit: With the script I already have, it does find the player’s character model fine, so it’s not that causing it not to work.

I’m confused, because its working for me? It’s removing my accessories fine.

Using script:

image

Not using script:

I tried it again with your script, and it doesn’t work on my side :confused:

Also, just to make clear that I am using your code in the script that is working, so the script IS running, but for some reason not removing my accessories.

game.Players.PlayerAdded:Connect(function(play) play.CharacterAdded:Connect(function(char) for _,v in pairs(char:GetChildren()) do if v:IsA("Accessory") then v:Destroy() end end end) end)

Try this and see if it works.

EDIT: Line breaks did not function as intended.

It works… kind of. It removed half my accessories. (hair and glasses are also accessory items)

helpme_aaaaaaaaaaaa

Edit: Its not working anymore for me…? Hmm I’m very confused

1 Like

It is not printing anything in the output neither in analysis

Did you make this a script on your game or did you use the command bar?

I put your script in a script in ServerScriptService, I didn’t use the command bar.

Could you try the original script with analysis and output?

Yes, I did try that. I put the script in the command bar too and it gave no output.

It looks like accessories are being added after your code is executed. Try something like this within your character added function:

local function check(instance)
  if instance:IsA("Accessory") or instance:IsA("CharacterMesh") then
    instance:Destroy()
  end
end

for _,  v in pairs(character:GetChildren()) do
   check(v)
end

character.ChildAdded:Connect(check)
3 Likes

Yay, it worked! For some reason I got some warnings in the output, but it did the job! :smile:

Thank you for the help!