Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Raycasting AI freezes when gets nil value if target suddenly disappeared by whatever reason is?

Asked by
Lequla 20
4 years ago

Ranged AI raycasting goes nil value if target suddenly disappeared by whatever reason.

I tried to make Ranged AI by my self with some online guide and wiki.. Thought I made everything correct but single error struggling me down.

Here are my codes


function checkSight(target) local ray = Ray.new(rootpart.Position, (target.Position - rootpart.Position).Unit * AttackRange) local part,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent}) if part then if part:IsDescendantOf(target.Parent) then return true end end return false end function BulletTrace() local Victim = nil for i,v in ipairs(workspace:GetChildren()) do local humanoid = v:FindFirstChild("Humanoid") local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart") if humanoid and torso and v.Name ~= script.Parent.Name and v.Name ~= Ally1 and v.Name ~= Ally2 then if (rootpart.Position - torso.Position).magnitude < sight and humanoid.Health > 0 then Victim = torso Victim.Parent.PrimaryPart = Victim end end end if Victim then return Victim.Parent:GetPrimaryPartCFrame(),CFrame.new(Victim.Parent.Humanoid.WalkToPoint).lookVector end end function Shoot() local VictimPPart,AimTrack = BulletTrace() local VictimPPart = VictimPPart.Position -- Line 150 local aim = VictimPPart+Vector3.new(math.random(-Accuracy*.008,Accuracy*.008),math.random(-Accuracy*.009,Accuracy*.009),math.random(-Accuracy*.1,Accuracy*.1))*(2+math.random())+AimTrack local OnHit,MyPosition = workspace:FindPartOnRay(Ray.new(barrel.Position,(aim - barrel.Position).Unit * (AttackRange * 2)), me) if VictimPPart then ... if OnHit then -- Line 185, causing issue sometimes aswell. local gnomed = OnHit.Parent:findFirstChildOfClass("Humanoid") if gnomed then gnomed:TakeDamage(damage) end else print("Hit failed :",me.Name) end ... end function Attack(target) if not reload then repeat local forwardVector = (target.Position - rootpart.Position).Unit local upVector = Vector3.new(0,1,0) local rightVector = forwardVector:Cross(upVector) wait(FireRate) for i = 0, 1, 0.1 do rootpart.CFrame = rootpart.CFrame:lerp(CFrame.fromMatrix(rootpart.Position, rightVector, upVector),i) end Shoot() if (rootpart.Position - target.Position).magnitude < RetreatMinRange then human.AutoRotate = false local c = rootpart.CFrame * CFrame.new(0,0,RetreatDistance) human:MoveTo(c.p) end human.AutoRotate = true until checkSight(target) == false or target == nil or target.Parent.Humanoid.Health < 1 -- Line 233 else reloading(true) end end

And the error is :

model.script:150: attempt to index nil with 'Position' Stack Begin
Stack Begin
Script 'model.script', Line 150 - function Shoot
Script 'model.script', Line 224 - function Attack
Script 'model.script', Line 106 - function pathToLocation
Script 'model.script', Line 243 - function main
Script 'model.script', Line 269
Stack End

model.script:233: attempt to index nil with 'Humanoid' Stack Begin
Stack Begin
Script 'model.script', Line 233 - function Attack
Script 'model.script', Line 106 - function pathToLocation
Script 'model.script', Line 243 - function main
Script 'model.script', Line 269
Stack End

Rarely, Line 185 'OnHit' making similar issue with Line 150 aswell. but difference is it gets nil value it self.

The error codes seems to be happen on '150' and '233' alot which seems to be relate with ray casting on BulletTrace and checkSight, but I don't know how I could fix this.

These error seems to be happen when the target suddenly died or evaded their shot with jumping down cliff or else.

Majority of bots are ok, but it happen occasionally with few of them.

doesn't these kinds of ray usually go over if there aren't any target found?

I've tried most of way I could imagine, like add some if line when it gets nil but didn't work with my tries. Now I don't get any idea how could I fix this. anyone help?

1 answer

Log in to vote
1
Answered by
Lequla 20
4 years ago

Never mind, I was dumb, Got answer as well.

moved several locals above "if VictimPPart then" to below and added more check solved issue. Sorry.

Ad

Answer this question