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

What's Wrong With This Hat Removal Script?

Well, the issue is a bit self explanatory. The script will not function. The print will output, but that's all. It's a simple script, and I'm not understanding why it's failing to function.

It's in a regular script, and is called upon whenever a value is changed.

game.Players.PlayerAdded:connect(function(player)
	repeat wait() until game.Lighting.IntroCamEnd.hatremoval.Value == true -- bool value
	print("Removing hats...") -- This print displays correctly.
	local hats = player:GetChildren()
	for i = 1, #hats do
		if hats[i].ClassName == "Hat" then
		hats[i]:Destroy()
	end
	end
end)

Thank you for reading, and like always, I look forward to finding a solution.

1 answer

✓ Accepted answer

You can't remove hats of a Player, you need to remove them of Character, look:

game.Players.PlayerAdded:connect(function(player)
    repeat wait() until game.Lighting.IntroCamEnd.hatremoval.Value == true -- bool value
    print("Removing hats...") -- This print displays correctly.
    for index, child in pairs (player.Character:GetChildren()) do
        if child.ClassName == "Hat" then
        child:Destroy()
    end
    end
end)

I changed the GetChildren loop, as the wiki reccomends.

2 comments

1

Oh, I hate it when forgetting one word nullifies the use of a whole script -- I'm surprised I didn't catch it by now. And that seems like a much better method than the one I was using before, and even if not more efficient, easier to read. Thank you for the solution and tip!

0

you are welcome. output is helpful if you want to know what is wrong with your script: http://wiki.roblox.com/index.php?title=Output

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