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