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

[SOLVED] Removing Hats Script -- Not working?

So, I have a script that manipulates multiple things inside the character and fires when someone enters the game, or respawns (or dies).

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(c)
	print'fetching player'
	repeat wait() until
	player.Character
	print'fetching character'
	local char = player.Character
	local hum = char.Humanoid
	local head = char.Head
	head.Transparency = 1
	print'invisible head and no name'
	hum.WalkSpeed = 50
	for _, v in pairs(char:GetChildren()) do
		wait()
	if v:IsA("Hat") then
		v:Destroy()
	end
	end
	print'removing hats'
	local h = Instance.new("Hint", game.Workspace)
	h.Text = "Welcome "..player.Name.." to GAMENAMEHERE"
	wait(5)
	h:remove()
	local h2 = Instance.new("Hint", player.PlayerGui)
	h2.Text = "All hats are removed so you get more anti-gravity effect"
	wait(5)
	h2:remove()
	print'welcome'
	
	local nograv = Instance.new("BodyForce", char.Torso)
	nograv.force = Vector3.new(0,2000,0)
	print'good to go'
	end)
	end)

So far, everything in this script is working well, except for one thing.

for _, v in pairs(char:GetChildren()) do
		wait()
	if v:IsA("Hat") then
		v:Destroy()
	end
	end

The above code is not firing.

Help?

1 answer

It is possible that the hat is being added to the character after your code has already executed. You may want to consider using a ChildAdded event handler to remove hats as they are added to the character.

char.ChildAdded:connect(function(child)
    if child:IsA("Hat") then
        child:Destroy()
    end
end)

1 comment

0

Thank you :)

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