Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make a script remove all accessories when the player joins?

I am trying to remove all the accessories from the player when they join however, It doesn't work. This is the code I've tried.

game.Players.PlayerAdded:Connect(function(player)
	local humanoid = player.Character:WaitForChild("Humanoid")
	local accessories = humanoid:GetAccessories()
	while accessories do
		accessories:Destroy()
	end
end)

Thanks in advance.

1 comment

0

Note: the local script is in StarterCharacterScripts

2 answers

You are on the right track. Something you can try is to make a server script in serverscriptservices that says the following:

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

This script will destroy any accessories the player has when its character is reloaded, such as when the player dies. If you want it for only when the player joins you'd revise the script:

game.Players.PlayerAdded:Connect(function(plr)
     local changed = Instance.new("BoolValue")
     changed.Parent = plr
     changed.Name = "IsDifferent"
     changed.Value = false
     plr.CharacterAdded:Connect(function(char)
          if plr.IsDifferent.Value == false then
               for i,v in pairs(char:GetChildren()) do
                    if v:IsA("Accessory") then
                         v:Destroy()
                    end
               end
               plr.IsDifferent.Value = true
          end
     end)
end)

I hope this answers your question

Edit:

I have found the solution! There is a tutorial provided by roblox right here on how to do this. You'll have to scroll down a bit but it is there. You can edit this script using the suggestions I provided before.

5 comments

0

I got an error saying Value is not a valid member of RBXScriptSignal at line 6 on the second script.

0

I found the error in my second script: Changed is a function, so I renamed the value to IsDifferent.

0

Don't use the 2nd parameter of Instance.new, it's slow and inefficient , use .Parent instead

0

@Nguyenlegiahung I edited my script to do so, thanks for suggesting that to both of us

0

The accessories are still not getting deleted, the error dissapeared but nothing happened

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	wait(2)
	local char = player.Character
	if char then
		local childs = char:GetChildren()
		for i,v in pairs(childs) do
			if v:isA("Accessory") then
				v:Destroy()
			end
		end
	end
end)

Detailed Explanation:

So basically what is happening in this script is that when the player joins the game it will get its character, a wait is added so we make sure we get the player, then we check if the character exists, we used :GetChildren to create a table that contains the children of the character (Head, UpperTorso, Hair, etc...).

Now we need to go through the table, so for i ,v in pairs. While going through the table if the item :isA("Accessory"), what this does is that it checks the class of the V (which is the child that we got from the character) and it filters it so now we got all the Accessory then we Destroy them .

7 comments

0

:isA() is supposed to be an uppercase I, not lowercase

0

I tried this and nothing came up in the output but none of the accessories got removed.

0

Oh I'll try that. Wait a second

0

Still nothing.

0

it worked fine for me tho idk whats your problem

0

Could you specify whether I am supposed to make a Server Script or a Localscript and where to put it ?

0

That's just a server script that i put in workspace, you can put it in Server Script Service or Server Storage ... definitely not local

This discussion is preserved as it was. New answers, comments, and votes are closed.