How to make section of code that runs if .MoveToFinished:Wait() waits for more than a certain amount of time?

Hi all,
I’m currently coding an AI that randomly walks and chases a player if they get in a certain range of the entity. However, I’m running into an issue with this part of my code:

humanoid.Humanoid.WalkSpeed = walkSpeed
humanoid.Humanoid:MoveTo(v.Position)
humanoid.Humanoid.MoveToFinished:Wait()

Where sometimes the entity would get stuck, so any code beyond humanoid.Humanoid.MoveToFinished:Wait() would never run. I want to fix this by adding a detector in which if humanoid.Humanoid.MoveToFinished:Wait() waits more than a certain amount of time, another section of code would run.

However, I don’t know how to get started coding this. My solution for this was this section of code

humanoid.Humanoid.WalkSpeed = walkSpeed
humanoid.Humanoid:MoveTo(v.Position)
if #humanoid.Humanoid.MoveToFinished:Wait() > 10 then
	task.wait(3)
	isAggrovated = false
	randomWalk()
else
	humanoid.Humanoid.MoveToFinished:Wait()
	isAggrovated = false
end

But it returned the error “Attempt to get length of boolean value”, so any help with this would be greatly appreciated. Thanks!

2 Likes

hey @TheAbsoluteSirius, this error is caused by this line

I don’t understand what you want to do here. Wouldn’t it be easier to create an attribute that would check whether an npc has reached a certain place?

2 Likes

Tried your solution out, and it seems to work better now. Thanks for the help!

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.