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

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

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)

2 comments

0

try local char = plr.Character or plr.CharacterAdded:Wait()

0

the wait worked thank you

1 answer

✓ Accepted answer

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)

3 comments

0

Plus taco's advice should lead to functional code

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

0

If statement to check if this part is the head. If it is not the head, set it's transparency

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