I need help debugging a script that doesn't destroy accessories

local function playerModifier(player)
	local character = player.Character
	if player.TeamColor == BrickColor.new("Navy blue") then
		character.Humanoid.WalkSpeed = 0
		character.Humanoid.JumpPower = 0
		for i,v in pairs(character:GetChildren()) do
			if v:IsA("Accessory") or v:IsA("Hat") then 
				v:Destroy() 
				print("ACCESSORY DESTROYED")
			end
		end
		for _, character in pairs (character:GetChildren()) do
			if character:IsA("BasePart") then
				character.Transparency = 1
			end
			print("FINISHED.")
		end
	end
end

players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function()
		giveTools(plr)
		playerModifier(plr)
	end)
end)

Before you say to remove giveTools(plr), this is the same script I used in https://devforum.roblox.com/t/i-need-help-debugging-a-script-that-gives-tools-according-to-their-teamcolor-when-they-join/2697158/5, so that is not a option.

The current issue is, the accessory part doesn’t destroy the player’s accessories when the join ingame and does not print out print("ACCESSORY DESTROYED") including errors, meaning there is something wrong with the code. I do not know what’s wrong with

for i,v in pairs(character:GetChildren()) do
			if v:IsA("Accessory") or v:IsA("Hat") then 

so I’m asking for help. I’d greatly appreciate the help received!

Maybe the player’s character hasn’t fully loaded yet, hence why it’s not destroying the players accessories. Try replacing local character = player.Character with local character = player.Character or player.CharacterAdded:Wait(). This might work.

Where am I supposed to put player.CharacterAdded:Wait()? I forgot to say that local character = player.Character is already there.

I also tried player.CharacterAdded:Wait() but now one of the prints print("FINISHED.") doesn’t show up and the player’s avatar doesn’t completely disappear into Transparency = 1.

It’s challenging to fix because Roblox typically triggers the CharacterAdded signal when the character is loaded. In your case, the accessory isn’t loaded when you attempt to remove it.

The only solution I see is to insert a task.wait() before the loop to wait a short period for the accessory to load.

Additionally, try not to use the same name for the character variable and the value given in the second loop. I’ve renamed it to basePart to address this issue as well.

local function playerModifier(player)
	local character = player.Character
	local humanoid = character:WaitForChild("Humanoid")
	if player.TeamColor == BrickColor.new("Navy blue") then
		humanoid.WalkSpeed = 0
		humanoid.JumpPower = 0
		
		task.wait()
		
		local characterContent = character:GetChildren()
		for i,v in pairs(characterContent) do
			if v:IsA("Accessory") or v:IsA("Hat") then 
				v:Destroy() 
				print("ACCESSORY DESTROYED")
			end
		end
		for _, basePart in pairs(characterContent) do
			if basePart:IsA("BasePart") then
				basePart.Transparency = 1
			end
			print("FINISHED.")
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(character)
		giveTools(plr)
		playerModifier(plr)
	end)
end)

If you have any questions, don"t hesitate to ask me!

When I copied and pasted the script, the accessories still didn’t get destroyed nor print out print("ACCESSORY DESTROYED"). While print("FINISHED.") does appear, the other one doesn’t. Is there something that has to do with

for i,v in pairs(characterContent) do
			if v:IsA("Accessory") or v:IsA("Hat") then 

?

No the code you sent here is correct. That’s working fine for me, so maybe add delay on the task.wait like 0.1s or 0.5s

Ok I tried it again but added a task.wait in here

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(character)
		giveTools(plr)
		task.wait()
		playerModifier(plr)
	end)
end)

This time it worked. Accessories got destroyed.

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