Issues related to Custom AI Behavior

Hello Developers!
I am making an AI which is a remake of “The Mimic” AI and I am facing some issues while developing it’s behavior.

Behavior

When the player reaches a SAFE zone, the AI goes to the last seen position of the player before he entered the SAFE zone (if the player is being chased).

Safezone Demonstration

sorry for the crappy quality

Issue

The problem is that the AI is focused on going to the safezone than chasing. In other words, The AI won’t chase any players until it has reached safezone.

Some more details and code snippets

I am using SimplePath for the pathfinding.
Each path instance has a Reached bindable event, which I used to make the AI pathfind to the safezone.

Where Reached is being used.

How would I solve this issue? I could use coroutines but I can’t think of any solution.
Thank you!

1 Like

A bump (need some help with the AI)

1 Like

Maybe what you could do is add a variable to the player to check if they are in the safe zone. If the player is in the safe zone then make the AI calculate a new path to the nearest player.

Try checking whether or not the player has left the safezone but using Region3 or workspace:GetPartsBoundInBox(), a more performance friendly option is by using BindableEvents.

Since you’re using SimplePath, you can try adding:

if not playerInSafezone then
   path.Reached:Wait()
end

after the repeat loop.

I was a little shocked to find that you’re having issues with simple path but I believe your methodology isnt in the right place;

What I typically do is do a while loop that checks if the magnitude of target is >5 then keep pathing for example in reference to self

while (selfpos-target.pos).mag>5 do

path:run()

end

you can add to the conditions of the while loop.

I don’t really use path.reached but I believe it is the root cause of your issue as it waits until you’ve done the full pathing (which is your issue). Once you remove that, you should be golden–after adding necessary conditions to break the movement

There is already a BoolValue SAFE added to the player once they join the game.
I also used the ZonePlus module to create the safe zones.
They check if the player has entered or exited the safe zone and set the values accordingly.

I think I forgot to mention this, but there is a GetPropertyChangedSignal on the IsChasing BoolValue, which fires a remote event to the client (in this case a target) if true or false, the screen will extend outwards when the AI is chasing them to let the player know that they’re being chased, and inwards when the AI has left chasing them.

IsChasing:GetPropertyChangedSignal("Value"):Connect(function()
	local val = IsChasing.Value;
	
	if (val) and (Target.Value ~= nil) then
		Humanoid.WalkSpeed = ChaseSpeed.Value;
		FoundEvent:FireClient(Util:PlayerFromCharacter(Target.Value), true)
		-- warn("FOUND");
		-- ...
	else
		Humanoid.WalkSpeed = WalkSpeed.Value;
		FoundEvent:FireAllClients(false);
			
		-- ...
	end;
end);

But with the example you provided, the AI stops chasing immediately when the player reaches the SAFE zone.
I want the AI to be able to chase even when moving to the last seen position of the player.

Updated Chase Function

you’d have to have a variable that keeps track of last targets position.

assume you have self = {}

self.LastKnownTargetPosition = vector3.zero

while chasing, update the lastknowntargetpos as the targets.primarypart.position until condition is met that prevents updating it for this target i.e. entering safezone, you’d also have to have the path:run() going to the lastknownpos. and having the magnitude check for lastknownpos

This works, but I want to the player to know that they’re not completely safe
To create this effect, The screen of the target does not return to its normal state until the AI reaches the safe zone, which I’m only able to do with path.Reached, but the AI is unable to chase during this part because of the yield that path.Reached:Wait() causes.
Any other solutions?

I suppose an alternative would be where

you have a ClosestTargetChecker thread where you check for the closest target

when a unique target is found - start up chase function, though you need to fire either a bindable or bool listening event where all current chases need to stop should a trigger be detected


Runservice.Stepped:Connect(function()

--check for best avail target

if newtarg found and newtarg ~=currenttarget then

StopChases:Fire()

currenttarget = newtarg

Chase()

end

end

would this work for you?

i think i was misinterpreting what you were asking, if thats the case disregard my earlier post;

I think it just comes down to conditional statements you put into place.

I’m sorry for not responding quickly.
But what if the target is the same as the one which entered the safe zone?

bump. I still require some help…

bump. I still require some help… (x2)

I suppose I don’t really know what you’re asking help for,

if target == TargetWhoJustEnteredSafezone then
--do code
end

Am I moving away from the topic?
You can read the main post again, that is my issue.
I want the AI to be able to go to the safezone and fire the event when it reaches it but also chase if it sees another player (could be the same one) while going to the last seen position.
The yield that path.Reached:Wait() causes an issue i.e. AI is too focused on going to the safezone than chasing other players (which the AI should be doing).
I want to get the best of both worlds, but am unable to think of a solution.

sorry I had thought we already solved the original solution or some of it based on previous responses

It comes down to conditionals or how you prefer to solve it. Perhaps add a wait timer before triggering return state , perhaps check until the target leaves a threshold of some studs before triggering return state. maybe you want this effect to occur when the enemy monster enters a threshold range.