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)
How do I get it to work after the player dies?
The script is supposed to remove the players accessories, but after the player dies they get their accessories back.
Your script only works when the player initially joins the game
local Players = game:GetService("Players")
-- you should use a function because they're reusable and help condense code
local function RemoveAccessories(char)
local childs = char:GetChildren()
for i,v in pairs(childs) do
if v:isA("Accessory") then
v:Destroy()
end
end
Players.PlayerAdded:Connect(function(player)
wait(2)
if player.Character then -- check if the player's character has already loaded
RemoveAccessories(player.Character)
end
player.CharacterAdded:Connect(RemoveAccessories) -- call the function when the player respawns
end)
local Players = game:GetService("Players")
local function RemoveAccessories(char)
print("removing character accessories") -- just something to let you know if the function is running
local childs = char:GetChildren()
for i,v in pairs(childs) do
if v:isA("Accessory") then
v:Destroy()
end
end
end -- added the extra 'end' here
Players.PlayerAdded:Connect(function(player)
wait(2)
if player.Character then
RemoveAccessories(player.Character)
end
player.CharacterAdded:Connect(RemoveAccessories)
end)
If the print isn’t working at all, it’s likely that Players.PlayerAdded isn’t firing. In which case, you should make a loop that goes through each player in the game and calls a function for each of them
local function OnPlayerAdded(player)
if player.Character then
RemoveAccessories(player.Character)
end
player.CharacterAdded:Connect(RemoveAccessories)
end
Players.PlayerAdded:Connect(OnPlayerAdded)
for _, plr in Players:GetPlayers() do
OnPlayerAdded(plr) -- call the function for each player
end
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(character)
task.wait(2)
local childs = character:GetChildren()
for i = 1,#childs do
if childs[i]:isA("Accessory") then
childs[i]:Destroy()
end
end
end)
end)
oh, sorry. Mines not working. Btw, my script is located in a folder in workspace, I don’t know if that changes anything but I like to keep things organized. Other than that idk why it wouldn’t work
Preferable you would want to keep scripts inside of server
script service because then people cannot see the scripts…
Just because of security reasons and other stuff.
Pretty sure this works, if you haven’t got a script working then make a new server script and place it inside of server script service, then lastly copy and paste this code inside:
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
player.CharacterAppearanceLoaded:Connect(function(character)
for _, part in pairs(character:GetChildren()) do
if part:IsA("Accessory") then
part:Destroy()
end
end
end)
player.CharacterAdded:Connect(function(character)
for _, part in pairs(character:GetChildren()) do
if part:IsA("Accessory") then
part:Destroy()
end
end
end)
end)