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

(Edited script) Problem with for loop to destroy hats on CharacterAdded?

Asked by
RoyMer 301 Moderation Voter
9 years ago

Edited the script, yet again there seems to be a problem with the for loop that Destroys the hats in the player (the second for loop)...

The second loop which destroys the hats of the player works fine in Roblox Studio but not in game.

player = game:GetService("Players").LocalPlayer
player.CharacterAdded:connect(function(character)

for i,v in pairs(game.StarterPlayer:GetChildren()) do
if v.ClassName == "Hat" and v.Name == game.StarterPlayer.HatSave.Value then
v.Parent = player.Character
end
end 

wait(3)
for i,v in pairs(character:GetChildren()) do
if v.ClassName == "Hat" and v.Name ~= game.StarterPlayer.HatSave.Value then
v:Destroy()
end
end

end)
1
Make sure it is running from a LocalScript because LocalPlayer can only be accessed from the client. 1waffle1 2908 — 9y
0
Thanks that was the problem! RoyMer 301 — 9y

2 answers

Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
9 years ago

It is possible that the hats have not loaded yet and are loading after you try to remove them. In that case, you could just add a short delay before removing the hats.

You're also using CharacterAdded from a LocalScript, which won't work because the character loads before the script. Instead, just use character=player.Character or character=player.Character or player.CharacterAdded:wait()

0
Thanks! That was the problem! RoyMer 301 — 9y
0
Actually, it had worked in studio, but not in game, now I edited the script, yet again it is working in studio but not in game... RoyMer 301 — 9y
Ad
Log in to vote
0
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

You shouldn't to use the CharacterAdded event in a LocalScript, given that they automatically reload along with the character...(when in the right services (PlayerGui..)).

Client

local player = game.Players.LocalPlayer
local character = player.Character

if not (character or character.Parent) then
    character = player.CharacterAdded:wait()
end

wait(1) -- slight delay, in case the hats take time to load

for _, child in pairs(character:GetChildren()) do
    if child:IsA("Hat") or child:IsA("Accoutrement") then
        child:Destroy()
    end
end

for _, child in pairs(playerGui:GetChildren()) do
    if child:IsA("Hat") then
        v.Parent = character
    end
end

Server

I'd recommend -- storing the hats in "ServerStorage", and cloning them to the character whenever they spawned instead of keeping them locally.

Pretty much works on the same logic, but makes use of the CharacterAdded and PlayerAdded events.

local function Refreshed(player)
    local character = player.Character
    local service = game.ServerStorage

    for _, child in pairs(character:GetChildren()) do
        if child:IsA("Hat") or child:IsA("Accoutrement") then
            child:Destroy()
        end
    end

    for _, child in pairs(service:GetChildren()) do
        if child:IsA("Hat") or child:IsA("Accoutrement") then
            v.Parent = character
        end
    end
end

game.Players.PlayerAdded:connect(function(player)
    player.Changed:connect(function(property)
        if (property == "Character") then
        wait(1) -- slight delay, in case the hats take time to load
            Refreshed(player)
        end
    end)
end)
0
It is not a local script, and I prefer not to use ServerStorage... By the way, edited the script, take a look again, the problem is with the removal of the hats from the player :( RoyMer 301 — 9y
1
It has to be a LocalScript because it's running on the client and accessing LocalPlayer. 1waffle1 2908 — 9y
0
Yeah, I was aware that you were having trouble with the removal, which is why I had one second waits before clearing the character... And as 1waffle1 said, (see previous reply). ImageLabel 1541 — 9y

Answer this question