I’m trying to make a boss fight, but it keeps throwing me this error: Workspace.???.Script:4: attempt to index nil with 'HumanoidRootPart'
Code:
while true do
wait(0.1)
for i,v in pairs(game.Players:GetChildren()) do
if math.floor((v.Character.HumanoidRootPart.Position-game.Workspace["???"].HumanoidRootPart.Position).Magnitude) <= 12 then
script.Parent.Humanoid:MoveTo(v.Character.HumanoidRootPart.Position)
end
if math.floor((v.Character.HumanoidRootPart.Position-game.Workspace["???"].HumanoidRootPart.Position).Magnitude) == 2 or math.floor((v.Character.HumanoidRootPart.Position-script.Parent.HumanoidRootPart.Position).Magnitude) == 1 then
script.Play.Disabled = false
end
end
end
add a :WaitForChild , like so v.Character:WaitForChild("HumanoidRootPart").Position? (waitforchild is better than using . if you aren’t sure it exists)
The debugger is telling you that v.Character is not existent.
You should also be using
game.Players:GetPlayers()
Your code should look like this:
while true do
wait(0.1)
for i,v in pairs(game.Players:GetPlayers()) do
local character = v.Character
if character then
if math.floor((character.HumanoidRootPart.Position-game.Workspace["???"].HumanoidRootPart.Position).Magnitude) <= 12 then
script.Parent.Humanoid:MoveTo(character.HumanoidRootPart.Position)
end
if math.floor((character.HumanoidRootPart.Position-game.Workspace["???"].HumanoidRootPart.Position).Magnitude) == 2 or math.floor((character.HumanoidRootPart.Position-script.Parent.HumanoidRootPart.Position).Magnitude) == 1 then
script.Play.Disabled = false
end
end
end
end