Hello, I am attempting to find the Position of a R6 Character’s Torso Part, and I get the error: “Attempt to index Nil with ‘Torso’.” I feel like I’m missing something huge here, But can’t figure it out. Here is My Code (Below)
--// Variables Pt.1
local hum = script.Parent.Humanoid
local myTorso = script.Parent.LowerTorso
local myRoot = script.Parent.HumanoidRootPart
--// Variables Pt.2
local char
--// Code
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(plr)
char = plr.Character or plr.CharacterAdded:Wait()
end)
function avoid()
hum:MoveTo(Vector3.new(0,0,math.random(-10,10)))
hum.MoveToFinished:Connect(function (look)
myRoot.CFrame = CFrame.lookAt(Vector3(char.Torso.Position.X, char.Torso.Position.Y, char.Torso.Position.Z)) --Error Occurs on this line, At the "char.Torso Parts."
end)
end
pcall(avoid)
I’ve been looking at countless DevForum post’s for about 2 hours, Doing TONS of Trial and error. All of them relate to this topic, But I’m always getting the before mentioned error. I can provide more details if need be.
Hello,
The first function (The Avoid one) is essentially part of the Boss’s A.I.
The 2nd one will be the VulnerableAttack Function,
The 3rd will LungeAttack,
4th will be LungeAttack02,
and the last one(s) will be the Stage transitions.
This will be controlled by a differention function which will be the “Brain” of the AI, using math.random to decide what to do.
I am getting the same error as before, even with wait for Child. (Updated Script)
--// Variables Pt.1
local hum = game.Workspace.Boss.Humanoid
local myTorso = game.Workspace.Boss.LowerTorso
local myRoot = game.Workspace.Boss.HumanoidRootPart
--// Variables Pt.2
local char
--// Code
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(plr)
char = plr.Character or plr.CharacterAdded:Wait()
end)
function avoid()
hum:MoveTo(Vector3.new(0,0,math.random(-10,10)))
hum.MoveToFinished:Connect(function (look)
myRoot.CFrame = CFrame.lookAt(Vector3.new(char:WaitForChild("Torso").Position.X, char:WaitForChild("Torso").Position.Y, char:WaitForChild("Torso").Position.Z)) --Error Occurs on this line, At the "char:WaitForChild("Torso").Position" Parts
end)
end
pcall(avoid)
The error is: “Attempt to Index nil with ‘WaitForChild’”
and No, Im hitting “Play” and Not “Run”. Just wanted to state this,
local Boss = game.Workspace:WaitForChild("Boss")
local hum = boss:WaitForChild("Humanoid")
local myTorso = Boss:WaitForChild("LowerTorso")
local myRoot = Boss:WaitForChild("HumanoidRootPart")
Still not Working. Same Error as Before
(“Attempt to index Nil with ‘Torso’”)
Script:
--// Variables Pt.1
local hum = game.Workspace.Boss.Humanoid
local myTorso = game.Workspace.Boss.LowerTorso
local myRoot = game.Workspace.Boss.HumanoidRootPart
--// Variables Pt.2
local char
--// Code
wait(2)
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(plr)
char = plr.Character or plr.CharacterAdded:Wait()
end)
function avoid()
hum:MoveTo(Vector3.new(0,0,math.random(-10,10)))
hum.MoveToFinished:Connect(function (look)
myRoot.CFrame = CFrame.lookAt(Vector3.new(char.Torso.Position.X, char.Torso.Position.Y, char.Torso.Position.Z)) --Error Occurs on this line, At the "char:WaitForChild("Torso").Position" Parts
end)
end
wait(2)
pcall(avoid)
The boss moves anywhere in between -10 and 10 studs, then when the Boss finished moving, The error popped up. I also tried it with “WaitForChild” But got the same error.
(“Attempt to index Nil with ‘WaitForChild’”)
I figured out the problem via. Debugging already.
Code:
local Boss = game.Workspace.Boss
local BossHum = Boss:WaitForChild("Humanoid")
local BossRoot = Boss:WaitForChild("HumanoidRootPart")
local character
local players = game:GetService("Players")
wait(3)
game.Players.PlayerAdded:Connect(function (plr)
character = plr.Character or plr.CharacterAdded:Wait()
end)
print(character) --Debug