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

What is wrong with this script that changes players' characters when they join the game?

Asked by
Zologo 37
6 years ago
Edited 6 years ago
game.Players.PlayerAdded:connect(function(player)

player.Character:connect(function(char)

   for name, child in pairs(char:GetChildren()) do
    if child:IsA('Accessory')then 
        child:Destroy()
    end 
    if child:IsA('BasePart')then
        child.Transparency = 1

    end
end

end)
end)

Keep getting this error: ServerScriptService.Script:3: attempt to index field 'Character' (a nil value)

0
try local char = plr.Character or plr.CharacterAdded:Wait() tacotown2 119 — 6y
0
the wait worked thank you Zologo 37 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

Firstly, instead of doing two ifs appended to eachother, do this:

if child:IsA('Accessory')then
       child:Destroy()
elseif child:IsA('BasePart')then
    child.Transparency = 1 
end

How I learned how to use else if Secondly, and the main problem here, is pairs(char:GetChildren()). Pairs is used to parse dictionaries, so it would be looking for a key-value pair like this: {["Roardorak"] = Character, ["Zologo"] = Character} Instead of a key-value pair like this, which is the one you're giving them (where the key is just the index in the array here: {RoardoraksCharacter, ZologosCharacter}. So the solution to your issue would be to simply add an "i" onto the pairs so that it treats the character children like the array that it is, instead of a dictionary.

These two blocks of code should run correctly, but I reccommend trying to code more like the second one in the future.

    game.Players.PlayerAdded:connect(function(player)

player.CharacterAdded:connect(function(char)

   for name, child in ipairs(char:GetChildren()) do
    if child:IsA('Accessory')then 
        child:Destroy()
    end 
    if child:IsA('BasePart')then
        child.Transparency = 1

    end
end

end)
end)

or

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        for name, child in ipairs(char:GetChildren()) do
                if child:IsA('Accessory') then 
                    child:Destroy()
            elseif child:IsA('BasePart') then
                    child.Transparency = 1
            end
        end
    end)
end)
0
Plus taco's advice should lead to functional code Roardorak 20 — 6y
0
Thank you Roar, I have another question, is there a way to make an exception like i want all BaseParts except the Head for example to have a transparency of 1 Zologo 37 — 6y
0
If statement to check if this part is the head. If it is not the head, set it's transparency Roardorak 20 — 5y
Ad

Answer this question