Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

[SOLVED] Removing Hats Script -- Not working?

Asked by 10 years ago

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

Log in to vote
1
Answered by
AxeOfMen 434 Moderation Voter
10 years ago

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)
0
Thank you :) keitheroni 45 — 10y
Ad

Answer this question