[Beginner Question] Need help with code and an explanation of something

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.

As long as your code works and you can understand it, nothing else matters. When you become more experienced with coding, you can begin focusing on things like formatting and readability.

Personally, I’d just do the following to remove accessories from players when they join.

game.Players.PlayerAdded:Connect(function(plr) --Running the function below when a player is added.
	plr.CharacterAdded:Connect(function() --Running the function below when the character is added.
		repeat wait(1) until plr.Character --Waiting until the character is fully loaded
		local character = plr.Character --Character variable
		
		for _, v in pairs (character:GetDescendants()) do --Looping through the character, finding any accessories and destroying them.
			if v:IsA("Accessory") then
				v:Destroy()
			end
		end
		
	end)
end)

Hope this helped!

1 Like

Thank you for the explanation! Personally I the whole time was thinking of… “If I understand the code for now… then why should I change it if it works.” But knowing that down the line I will find other methods of doing things more efficiently, it all comes with experience.

BTW… I figured out why the 2nd code wasn’t working… before you send me the code you had I forgot to add a wait for the character to be loaded in, so I did a task.wait before iterating through the characters children. But thanks for demonstrating your code!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.