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

Why isn't my script for removing hats working correctly?

I made a script so that when a player enters, its hats will be deleted. Here is the script:

game.Players.PlayerAdded:Connect(function(player)
	local char = player.Character
	if char then
		local count = char:GetChildren()
		for i = 1, #count do
			if (count[i].className == "Accessory") then
				count[i]:Destroy()
			end
		end
	end
end)

Also, click me to see where the script is in.

Hope someone can help me fix the problem!

1 comment

0

Hi, thanks to those people who gave upvotes! :)

2 answers

When a player is added the char variable is defined very quickly, not allowing the character to load. So the hats remover script part doesn't execute.

You should use the player.CharacterAdded function which will be fired each time a player dies.

The fixed script would be

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local count = char:GetChildren()
		for i = 1, #count do
			if (count[i].className == "Accessory") then
				count[i]:Destroy()
			end
		end
	end)
end)

If this answered your question then don't forget to click the Accept Answer button.

2 comments

0

It still doesn't work

0

So apparently it passes through the for loop but the if loop isnt doing its thing

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