Accessory Removal

Hey!

I’ve made a script, that removes player’s accessories, once they click a button. However, I don’t want all of their accessories to be removed, only some of them.

Is there any way of how could I edit my script, to make exempt (bypassed) accessories, so they won’t get removed?

The code:

function onTouched() 
	local player = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent
	local d = player.Character:GetChildren() 
	for i=1, #d do 
		if (d[i].className == "Accessory") then
			local exempt = d[i].AccessoryType == "Hair" and d[i].AccessoryType == "Face"
			if not exempt then
				d[i]:remove()
			end
		end 
	end  
end

script.Parent.Parent.Parent.MouseButton1Click:connect(onTouched)

So once you successfully remove only 1 accessory from the player you can just break from the function.

function onTouched() 
	local player = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent
	local d = player.Character:GetChildren() 
	for i=1, #d do 
		if (d[i].className == "Accessory") then
			local exempt = d[i].AccessoryType == "Hair" and d[i].AccessoryType == "Face"
			if not exempt then
				d[i]:remove()
                break -- Once 1 accessory is removed, stop the loop.
			end
		end 
	end  
end

script.Parent.Parent.Parent.MouseButton1Click:connect(onTouched)

local player = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent

You should use script:FindFirstAncestorOfClass("Player") or find a better way to get the player. Maybe by relocating the script to be more easily found, if available?

thats not the issue, player variable is working fine, thanks tho

thank you, works exactly as I wanted it to work!

From this line, you probably want to use or instead of and? I assume that they are two different things. Otherwise, this will be always false

i don’t think it matters (if its “or” or “and”), because it is currently working even with and

oh wait, you’re right, it has to be “or”

1 Like

Try keep clicking that button until the player has nothing else to remove except “Hair” and “Face” accessory. If Hair accessory is also a Face accessory then you are good. In case that there are 2 different things, those will be removed at some points also.

There ya go haha. Glad I could help you with this. Enjoy coding!

oh wait… what I meant was that once player clicks a button, all of his accessories should be removed, except from the exempt ones (currently hair and face accessories)

Then if you change from and to or. It should be working properly as you want. The reason that it didn’t work because the exempt value was always false. Also, remove the break that I just added from earlier.

sorry for still bothering you :sweat_smile:, but now its indeed removing all accessories, though it doesn’t care about the exempt ones anymore (it removes all accessories)

Then remove all if statement for exempt value, remove the break command, and make sure at least that what you are about to remove is an accessory.

Try this

function onTouched()
    local player = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent
    local accessories = player.Character:GetChildren()
    local exemptedTypes = {"Hair", "Face"}

    for i = 1, #accessories do
        if accessories[i].className == "Accessory" then
            local accessoryType = accessories[i].AccessoryType
            local isExempted = false

            for j = 1, #exemptedTypes do
                if accessoryType == exemptedTypes[j] then
                    isExempted = true
                    break
                end
            end

            if not isExempted then
                accessories[i]:Remove()
            end
        end
    end
end

script.Parent.Parent.Parent.MouseButton1Click:Connect(onTouched)

this script still removes all the accessories, I think its due to the accessories[i]:Remove() which removes all accessories no matter what

What AccessoryTypes are you wearing?

i tried it with Face, Hair, Shoulder & Front and then also with just Hair & Face. Both times all of the accessories got removed.

I compared an enum type to a string woopsies, here is the corrected version

function onTouched()
    local player = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent
    local accessories = player.Character:GetChildren()
    local exemptedTypes = {"Hair", "Face"}

    for i = 1, #accessories do
        if accessories[i].className == "Accessory" then
            local accessoryType = accessories[i].AccessoryType.Name
            local isExempted = false

            for j = 1, #exemptedTypes do
                if accessoryType == exemptedTypes[j] then
                    isExempted = true
                    break
                end
            end

            if not isExempted then
                accessories[i]:Remove()
            end
        end
    end
end

script.Parent.Parent.Parent.MouseButton1Click:Connect(onTouched)

yep, works perfectly as i want it to work, many thanks!

1 Like