Patrol not working

What do you want to achieve? When the dummy cant see the player and is not suspicious of the player, go back to patrolling

What is the issue? dummy does not go back to patrolling
Video Proof:

--Services
local plr = game:GetService("Players")
local repStorage = game:GetService("ReplicatedStorage")
local runServ = game:GetService("RunService")
--Modules

local funcModule = require(repStorage.funcModule)
--Variables
local dummy = script.Parent
local humRP = dummy:WaitForChild("HumanoidRootPart")
local count = 0
local run
local notLookingEvent	
local lookingEvent 
--Dummy

local dFOV = dummy:WaitForChild("Head").DummyFov
--Nodes

local startingNode = workspace.Node1
local currentNode = nil
local adjacentNodes = nil
local nextNode = nil
--Settings

local Settings = {       --Stores the dummies settings
	patrolMode = true,
	idle = false,
	suspicion = false,
}
--Tween

local info = TweenInfo.new(2)
local goal = {}
--RayCast

local rayOrigin = dummy.Head.Position
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {dummy, dFOV,}
raycastParams.IgnoreWater = true
--Functions

local function moveAI()
	adjacentNodes = currentNode:GetChildren() -- get its adacent nodes
	nextNode = adjacentNodes[1].Value -- get next node
	print(nextNode) -- debugging
	goal.Position = nextNode.Position -- makes a goal for the tween
	return funcModule:createTween(humRP, info, goal) -- creates the tween
end

local function resetNodes(nextNode) 
	if Settings.suspicion then -- if dummy is suspicious of the player
		nextNode = nil -- reset next node and adjacent nodes
		adjacentNodes = nil
	else	
		currentNode = nextNode -- current node is the next node
		nextNode = nil -- reset next node
		adjacentNodes = nil -- reset adjacent nodes
	end
end

local keepOnPrinting = coroutine.create(function()
	while true do
		wait(3)
		print(Settings.patrolMode)
		print(Settings.suspicion)
	end
end)
--Mainline
coroutine.resume(keepOnPrinting) -- debugging

lookingEvent = dFOV.Touched:Connect(function(hit) -- when triangle touched
	local runEvent 
	local plrHit = hit.Parent
	local humanoidRootPart = plrHit:FindFirstChild("HumanoidRootPart") -- get the player's humRP
	local plrHead = plrHit:FindFirstChild("Head")
	if humanoidRootPart and humanoidRootPart:WaitForChild("Suspicious").Value then -- if the object that touched the triangle is a player and they are suspicion
		Settings.suspicion = true -- guard is suspicious of the player
		Settings.patrolMode = false -- stop moving
		runEvent = runServ.Heartbeat:Connect(function() -- every frame do:
			local raycastResult = workspace:Raycast(rayOrigin, plrHead.Position - rayOrigin, raycastParams) -- create a raycast from dummy head to player
			if raycastResult and raycastResult.Instance.Parent then								
				if raycastResult.Instance.Parent == plrHit or raycastResult.Instance.Parent.Parent == plrHit then -- check if the instance's parent is the player
					dummy:SetPrimaryPartCFrame(CFrame.lookAt(dummy:WaitForChild("HumanoidRootPart").Position, humanoidRootPart.Position * Vector3.new(1,0,1) + dummy:WaitForChild("HumanoidRootPart").Position * Vector3.new(0,1,0))) -- look at the player
				else -- if it isnt
					runEvent:Disconnect() -- disconnect the function
					Settings.suspicion = false -- guard stop being suspicious
					Settings.patrolMode = true -- start moving again
				end
			end
		end)
	end
end)

while Settings.patrolMode and not Settings.suspicion do -- repeats if the dummy is on patrol mode
	wait(3)
	if currentNode == nil then -- checks if there is a current node
		print("No current node")
		currentNode = startingNode -- if not changes current node to starting node
		local track = moveAI()
		if Settings.suspicion then
			print("puase") -- debugging
			track:Pause()
			count -= 1
		else
			track:Play()
		end
		resetNodes(nextNode)
	else -- if there is already a current node
		print("There is a current node")
		local track = moveAI()	
		if Settings.suspicion then
			print("puase") -- debugging
			track:Pause()
			count -= 1
		else
			track:Play()
		end
		resetNodes(nextNode)
	end
end
1 Like