How to get a player's character from ServerScriptService

I’m trying to get the player’s character. This is my code. The error message is “attempt to index nil with ‘Character’”

    for _,player in pairs(game.Players:GetPlayers()) do
    	local Character = player.Character
    	local Torso = Character:FindFirstChild("Torso")
    	
    	Torso.BrickColor = BrickColor.White()
  end
1 Like

Try using Players:GetChildren().

:GetPlayers() is pretty much the same as :GetChildren(), but it only gets the players. Also, I have tried that.

I think it’s because you named the variable the exact same as the property name. You see the 2 same wordsmith Character? I think this makes the script thinks that you’re referencing the variable. So I think you should change the variable name to char or something else.

It doesn’t work. I got the same result.

Have you made sure the all the reference is the same? Make sure it named as char at every variable name that involves the character. If still doesn’t work, show us the full script.

You need to use ipairs instead of pairs, since you only use pairs in dictionaries.

Are you sure? I mean ipairs are for arrays and not keys and value.

Just change the underscore (_,players) to (i,players)

Doesn’t work. Again, got the same result.

I think it’s better if you show us the full script, because there’s an indentation of the for loop line, and I’m guessing there’s a full script behind it.

local module = {}

function module.OnRan()
for i,player in ipairs(game.Players:GetPlayers()) do
	local Character = player.Character
		local UpperTorso = Character:FindFirstChild("UpperTorso")
	
	UpperTorso.BrickColor = BrickColor.White()
	end
end




return module

I used this and it works just fine.
local Players = game:GetService(“Players”)

wait(1)
for i,player in pairs(Players:GetPlayers()) do
--if (player ~= nil) then
	local Character = player.Character
	if (Character:FindFirstChild("HumanoidRootPart")) and (Character.HumanoidRootPart.BrickColor ~= BrickColor.White()) then
		local Torso = Character:FindFirstChild("HumanoidRootPart")

		Torso.BrickColor = BrickColor.White()	
		--end 
	end

end

Change it so if the character doesn’t exist, you wait for the .CharacterAdded event.

local Character = player.Character or player.CharacterAdded:Wait()
1 Like

My bad, I got those mixed up. .

1 Like