Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Break a wait() so it follows? (simple AI)

Hello, i am making a pretty simple AI and im trying to make it so it wander around a point and when a player is close enough, it walks towards the player, that part is working, only, when it is stopped on a spot while "wandering" there is a random wait time (5,10) and it wont start following the player because it is waiting for this to finish, how can i make it break its waiting time when the player is close enough?

while true do
	wait(.5)
	local target = findNearestTorso(script.Parent.Torso.Position)
	local torso = script.Parent.Torso
	local dist = (torso.Position - point.Position).Magnitude
	if target ~= nil and dist < (math.random(30,35)) then
		script.Parent.Humanoid.WalkSpeed = 15
		script.Parent.Humanoid:MoveTo(target.Position, target)
	else
		script.Parent.Humanoid.WalkSpeed = 10
		script.Parent.Humanoid:MoveTo(Vector3.new(math.random(point.Position.x-15,point.Position.x+15),0,math.random(point.Position.z-15,point.Position.z+15)), point)
		if target == nil then
			wait(5,10)   --[[HERE]]
		end
	end
end 

1 answer

✓ Accepted answer

You should put it into a loop, like this:

if not target then --you can just put this instead of "if target == nil"
	local waitTime = math.random(5,10) --how long you wait for
	local waiting = 0 --how long you've waited
	while waiting < waitTime do
		target = findNearestTorso(script.Parent.Torso.Position) --you have to reset this whenever the loop runs
		if target then --if target is actually a thing now
			break
		end
		wait(.1) --waits
		waiting = waiting + .1
	end
	if target then --if target is actually a thing now
		break
	end
end

1 comment

0

thanks! just had to remove the lines 12 to 14 and it worked perfectly!

This discussion is preserved as it was. New answers, comments, and votes are closed.