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?
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.
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 , but now its indeed removing all accessories, though it doesn’t care about the exempt ones anymore (it removes all accessories)
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)
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)