So am a not good scripter but, I am trying. I was following a tutorial and that error appeared during testing so any help please?
function findTarget() local dist = 200 local target = nil for i,v in ipairs(workspace:GetChildren()) do local human = v:FindFirstChild("Humanoid") local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart") if human and torso and v.Name ~= script.Parent.Name then if (myRoot.Position - torso.Position).Magnitude < dist and human.Health > 0 then target = torso dist = (myRoot.Position - torso.Position).Magnitude end end end return target end function checkSight(target) local ray = Ray.new(myRoot.Position, (target.Position - myRoot.Position).Unit * 200) local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent}) if hit then if hit:IsDescendantOf(target.Parent) then return true end end return false end
And the error is : 90: attempt to index nil with 'Position' - Server - AI:90 18:43:06.623 Stack Begin - Studio 18:43:06.623 Script 'Workspace.Knight.AI', Line 90 - function checkSight - Studio - AI:90
Any help is appretiated!
i think you forgot to make the variable myRoot
local myRoot = script.Parent:WaitForChild("HumanoidRootPart") function findTarget() local dist = 200 local target = nil for i,v in ipairs(workspace:GetChildren()) do local human = v:FindFirstChild("Humanoid") local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart") if human and torso and v.Name ~= script.Parent.Name then if (myRoot.Position - torso.Position).Magnitude < dist and human.Health > 0 then target = torso dist = (myRoot.Position - torso.Position).Magnitude end end end return target end function checkSight(target) local ray = Ray.new(myRoot.Position, (target.HumanoidRootPart.Position - myRoot.Position).Unit * 200) local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent}) if hit then if hit:IsDescendantOf(target.Parent) then return true end end return false end
Nope my friend I already made the variable you need the whole script ? Here it's :
-- Main body parts local myRoot = script.Parent:WaitForChild("HumanoidRootPart") local myHuman = script.Parent:WaitForChild("Humanoid") local rightHand = script.Parent:WaitForChild("RightHand") local upperTorso =script.Parent:WaitForChild("UpperTorso") -- Weapon local sword = script.Parent:WaitForChild("Sword") local swordweld = sword:WaitForChild("SwordWeld") --Sounds local lungeSound = sword:WaitForChild("Lunge") local slashSound = sword:WaitForChild("Slash") local unsheathSound = sword:WaitForChild("Unsheath") --AI booleans local attackCool = true local swordHolstered = false local chasing = false local pathFailcount = 0 --Animation local leftSlash = script.Parent:WaitForChild("LeftSlash") local leftSlashAnimation = myHuman:LoadAnimation(leftSlash) leftSlashAnimation.Priority = Enum.AnimationPriority.Action local rightSlash = script.Parent:WaitForChild("RightSlash") local rightSlashAnimation = myHuman:LoadAnimation(rightSlash) rightSlashAnimation.Priority = Enum.AnimationPriority.Action --Reaspawn local clone = script.Parent:Clone() function getUnstuck() myHuman:Move(Vector3.new(math.random(-1,1), 0, math.random(-1,1))) wait(0.3) end function wander() local xRand = math.random(-50,50) local zRand = math.random(-50,50) local goal = myRoot.Position + Vector3.new(xRand, 0, zRand) local PathfindingService = game:GetService("PathfindingService"):CreatePath() PathfindingService:ComputeAsync(myRoot.Position, goal) if PathfindingService.Status == Enum.PathStatus.Success then pathFailcount = 0 local waypoints = PathfindingService:GetWayPoints() for i,v in ipairs(waypoints) do if v.Action == Enum.PathWaypointAction.Jump then myHuman.Jump = true end myHuman:MoveTo(v.Position) if i % 5 == 0 then if findTarget() then break end end local moveSuccess = myHuman.MoveToFinished:Wait() if not moveSuccess then break end end else pathFailcount = pathFailcount + 1 if pathFailcount == 10 then pathFailcount = 0 getUnstuck() end end end function findTarget() local dist = 200 local target = nil for i,v in ipairs(workspace:GetChildren()) do local human = v:FindFirstChild("Humanoid") local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart") if human and torso and v.Name ~= script.Parent.Name then if (myRoot.Position - torso.Position).Magnitude < dist and human.Health > 0 then target = torso dist = (myRoot.Position - torso.Position).Magnitude end end end return target end -- THE ERROR HAPPENS HERE .... function checkSight(target) local ray = Ray.new(myRoot.Position, (target.Position - myRoot.Position).Unit * 200) local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent}) if hit then if hit:IsDescendantOf(target.Parent) then return true end end return false end -- BETWEEN THIS COMMENT .... function checkHeight(target) if math.abs(myRoot.Position.Y - target.Position.Y) < 3 then return true end return false end function checkDirect(target) if checkSight() and checkHeight() and (myRoot.Position - target.Position).Magnitude < 30 then return true end return false end function pathToTarget(target) local path = game:GetService("PathfindingService"):CreatePath() path:ComputeAsync(myRoot.Position, target.Position) if path.Status == Enum.PathStatus.Success then pathFailcount = 0 local waypoints = path:GetWayPoints() for i,v in ipairs(waypoints) do if v.Action == Enum.PathWaypointAction.Jump then myHuman.Jump = true end spawn(function() wait(0.5) if myRoot.Position.Y < myHuman.WalkToPoint.Y and chasing == false then myHuman.Jump = true end end) myHuman:MoveTo(v.Position) local moveSuccess = myHuman.MoveToFinished:Wait() if not moveSuccess then break end if i % 5 == 0 then if checkDirect(target) then break end if target ~= findTarget() then break end end if (waypoints[#waypoints].Position - target.Position).Magnitude > 30 then break end end else pathFailcount = pathFailcount + 1 if pathFailcount > 10 then pathFailcount = 0 getUnstuck() end end end function chase(target) myHuman.WalkSpeed = 20 chasing = true myHuman:MoveTo(target.Position) end function main() chasing = false myHuman.WalkSpeed = 16 local target = findTarget() if target then local targetDistance = (myRoot.Position - target.Position).Magnitude if checkDirect(target) then if targetDistance > 3 then chase(target) end elseif checkSight(target) and math.abs(myRoot.Position.X - target.Position.X) < 1 and math.abs(myRoot.Position.Z - target.Position.Z) < 1 then getUnstuck() else pathToTarget(target) end else wander() end end while wait(0.1) do main() end
hey you! have you ever heard of enes? if you are in trouble, better call enes!