Trouble with a searching function

I’m trying to write a function that moves an npc to a random position, and after getting to that position, waits a second, and runs the function again. The problem is that every thing that I tried hasn’t worked so far. Here’s all the methods I’ve tried:

function EnemyAI:StartSearch(enemy,variable,cooldown,range,offsetpos)
	local hum = enemy:FindFirstChild("Humanoid")
	local hrp = enemy:FindFirstChild("HumanoidRootPart") or enemy.PrimaryPart
	variable = true
	
	local function Move(hrpv,humv)
		humv:MoveTo(hrpv.Position + Vector3.new(math.random(-range,range),0,math.random(-range,range)))
		humv.MoveToFinished:Connect(function()
			wait(cooldown)
			Move(hrpv,humv)
		end)
	end
	
	coroutine.wrap(Move)(hrp,hum)
end
function EnemyAI.StartSearch(enemy,variable,cooldown,range,offsetpos)
	local hrp = enemy:FindFirstChild("HumanoidRootPart") or enemy.PrimaryPart
	local hum = enemy:FindFirstChild("Humanoid")
    variable = true
	coroutine.wrap(function()
		local restart = true
		while restart do
			restart = false
			hum:MoveTo(hrp.Position + Vector3.new(math.random(-range,range),0,math.random(-range,range)))
			hum.MoveToFinished:Connect(function()
				wait(cooldown)
				restart = true
			end)
		end
	end)
end
function EnemyAI.StartSearch(enemy,variable,cooldown,range,offsetpos)
	local hrp = enemy:FindFirstChild("HumanoidRootPart") or enemy.PrimaryPart
	local hum = enemy:FindFirstChild("Humanoid")
    variable = true
	coroutine.wrap(function()
		local restart = true
		while restart do
			restart = false
			hum:MoveTo(hrp.Position + Vector3.new(math.random(-range,range),0,math.random(-range,range)))
			hum.MoveToFinished:Wait()
			hum.MoveToFinished:Connect(function()
				wait(cooldown)
				restart = true
			end)
		end
	end)
end

I can’t think of anything else to try please help. Thank you in advance.

2 Likes

Maybe humanoid mvoetofinished isnt firing

2 Likes

I thought so to, but I checked by printing random stuff and it printed, so its not that.

2 Likes

what doesnt wokr btw, does the AI not get to the random position, or does it just not move again after arriving?

2 Likes

For the first method it starts out working fine, but after like, 30 seconds the enemy starts to move constantly, rather than waiting for the movement to finish. For the other 2 the enemy just doesn’t move.

Edit:
I’ve done a few more tests and found some new things. Rather than not moving at all, the last two methods only make the enemy move once (I forgot to run the coroutines in those ones), although in the second method the movetofinished event is firing, while in the third method it isn’t.

2 Likes

i think the reason it isnt firing in the third one is because of the

humanoid:MoveToFinished:Wait()
1 Like

Yeah, I realized that. Although I still don’t know how to make one of the other two methods work.

1 Like

try changing to this on third method

while restart do
	restart = false
	hum:MoveTo(hrp.Position + Vector3.new(math.random(-range,range),0,math.random(-range,range)))
	hum.MoveToFinished:Wait()
	wait(cooldown)
	restart = true
	
end
1 Like

It worked! Thank you so much for your help! :grinning:

1 Like

Epicccc good luck on your development!!

1 Like

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