Error atempt index to nil torso

I got error which is atempt index to nil torso

Then my script will broke

Can somebody help me to fix it.

The error is indicating the parent of Torso is nil, which means that the player’s Character doesn’t exist at that given moment. In this case, a check can be made to ensure the player’s Character exists before trying to index one of its parts:

local Players = game:GetService("Players")

for _, player in ipairs(Players:GetPlayers()) do -- :GetPlayers() is a built-in method of the Players Service that should be used for this instead of :GetChildren()

    local Character = player.Character or player.CharacterAdded:Wait()
    local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart") -- Checking for the HumanoidRootPart is more reliable since both R6 & R15 Characters have one & it's also the PrimaryPart of the Character

    if Character and HumanoidRootPart then
        -- Update CFrame here since the Character & its HumanoidRootPart exist
    end
end

if I’m not mistaken, I’ll put that at the top of the script?

It wouldn’t go at the top of the script since the image you posted looped through the Players service on line 149 – the loop in the provided codeblock above would replace/be added onto lines 149-151 from the OP since it’s essentially the same thing except with a conditional statement to ensure it doesn’t error if the Character does not exist.

Torso only works for r6 and UpperTorso works for r15 idf you use HumanoidRootPart it can be used for r6 and r15

My game is r6 not r 15 so im correct.

Hi, the best solution to solve this issue is just check if the player character is not nil and if the Torso is here. And for teleporting a player character change CFrame of HumanoidRootPart and not the CFrame of the Torso because the HumanoidRootPart is the Primary part of the character model.

For check this you can just change your code to this:

for _, plr in pairs(Player:GetPlayers()) do
  local char = plr.Character
  local root = char and char:FindFirstChild("HumanoidRootPart") -- this will check if the player character is not nil and if the player character is nil root will be nil else root will be the value returneed by FindFirstChild function
  
  if root then
      root.CFrame = Spawns[math.random(1, #Spawns)].CFrame
  end
end
1 Like

use v:WaitForChild(“Character”).Torso.CFrame instead of v.Character.Torso.CFrame

if that didnt work use

for i, v in pairs(Players:GetChildren()) do 
 if v:IsA("Model") then
  v.HumanoidRootPart.CFrame = Spawns[math.random(1,#Spawns)].CFrame
 end
end

Change it to a WaitForChild. You never know. It might have not loaded after the character was added.

Both of your scripts won’t work. Debugging your first one.
Character: A Property of player.
WaitForChild: Waits for Children (like parts or folders), not properties (like Character, UserId, Name etc…)
Your second one:
v is = to the player.
v can never be a model. v is always a Player, since it’s “GetPlayers”.
To fix both scripts, you need to do:
v.CharacterAdded:Wait() – wait until character exists
local Char = v.Character

sorry im new to scripting thats why all these mistakes

2 Likes

Well, you’ll get better if you practice and learn by watching people and finding resources (like Roblox’s Beginner Scripting playlist).

Thanks dude also very good explanation! you fix my issue.

1 Like