Okay so I need help with two pieces of code. That do practically the same of removing a players accessories upon joining the game. But the thing is that one other developer recommended me to do the other one, which first of all doesn’t really work unless someone solves it for me. And I just need to know if my code is bad practice or something even tho I fully understand it when I think through it and write…, idk have a look for yourself.
So here is my code that works.
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
task.wait()
if player.Character then
print("Found Character")
for i, ob in pairs(character:GetChildren()) do
if ob:IsA("Accessory") or ob:IsA("Hat") then
ob:Destroy()
end
end
end
end)
end)
And here is the other code I have tried that doesn’t give any errors but I can’t really see any flaws in it either.
game:GetService("Players").PlayerAdded:Connect(function(player)
local function charAdded(character)
for i, object in pairs(character:GetChildren()) do
if object:IsA("Accessory") or object:IsA("Hat") then
object:Destroy()
end
end
end
if player.Character then
charAdded(player.Character)
end
player.CharacterAdded:Connect(charAdded)
end)
But as well I find the 2nd code harder to read for some reason. But as well I would like to know why my 2nd code doesn’t work.