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)
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)