I have an NPC where I need it to have different states, I have one state where it is suspicious and walks to your latest position, but how do I test if he walks up to you latest position that In that period of time he checks to see if you're in his detection box and if you are he chases you. But if your not he goes back to walking around points

Where I'm at right now is I go in his detection box walk out he goes into the suspicious mode and then goes back, BUT the second time I walk up he goes into chasing mode no matter what!

Code:

hmm = 0
De = true
What = false
local hum = script.Parent.Guard.Humanoid
local GN = script.Parent.Guard
local Ac = GN.Head.Ac.A -- Billboard gui
loop = false





script.Parent.Guard.DET.Touched:Connect(function(hit)
if De == true then
	


if hit.Parent.Name == "Gun" then -- checks to see if the tool your holding is in detection box (It is auto equipped)
	De = false
script.Parent.Value.Value = false
hum:LoadAnimation(hum.Parent.Walk):Stop()	
hum:LoadAnimation(hum.Parent.What):Play()
hum.WalkSpeed = 0
wait(2)
hum.WalkSpeed = 16
hum:LoadAnimation(hum.Parent.Walk):Play()	
hum:LoadAnimation(hum.Parent.What):Stop()
hum:MoveTo(hit.Position)
Ac.TextColor3 = Color3.fromRGB(231, 255, 14)
Ac.Text = "?"
hmm = hmm + 1
loop = true -- opens loop hole for second detection
wait(3)
script.Parent.Guard.DET.Touched:Connect(function()  -- checks if touched
if loop == true then -- checks if loop is open
	Ac.TextColor3 = Color3.fromRGB(255, 0, 4)
	Ac.Text = "!"
	hum.WalkSpeed = 30
	hum:LoadAnimation(hum.Parent.Walk):Stop()
	hum:LoadAnimation(hum.Parent.OHNO):Play()
	while true do
		wait(.5)
		local newpos = hit.Position
		hum:MoveTo(newpos)
		
		
		
	end
	end
end)
	
end
loop = false
Ac.Text = " "
script.Parent.Value.Value = true -- goes back to normal
De = true
hmm = 0
  
end
end)