Guard patrol not working

What I want the code to do is to move between nodes and stop and look at the player whenever the guard can see them. The guard does see them and look at the player and stops patrolling, however after leaving the fov of the guard, the guard doesn’t start patrolling again.

Video of whats happening:

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

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

local dFOV = dummy:WaitForChild("Head").DummyFov
--Nodes
local currentNode = nil
local startingNode = workspace.Node1
local adjacentNodes = nil
local nextNode = nil
--Settings

local Settings = {       --Stores the dummies settings
	patrolMode = true,
	idle = nil,
	suspicion = nil,
}
--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
	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
		nextNode = nil
		adjacentNodes = nil
	else
		currentNode = nextNode -- current node is the next node
		nextNode = nil -- reset next node
		adjacentNodes = nil -- reset adjacent nodes
	end
end

lookingEvent = dFOV.Touched:Connect(function(hit) -- if a part touches the dFOV
	Settings.suspicion = true -- get suspicious of player
	local runEvent
	local plrHit = hit.Parent -- gets the parent of the part hit
	local humRP = plrHit:FindFirstChild("HumanoidRootPart")
	local plrHead = plrHit:FindFirstChild("Head") -- gets the plrs head
	if humRP then
		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 there is a result and the part hit has a parent
				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, humRP.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
				end
			end
		end) 
	end
end)

--Mainline
while Settings.patrolMode do
	wait(3)
	if currentNode == nil then
		currentNode = startingNode
		local track = moveAI() -- create a track of the movement
		track:Play() -- play the track
		if Settings.suspicion then
			track:Pause()
		end
		resetNodes(nextNode) -- reset the nodes
	else
		local track = moveAI() -- create a track of the movement
		track:Play() -- play the track
		if Settings.suspicion then
			track:Pause()
		end
		resetNodes(nextNode) -- reset the nodes
	end
end

You have it paused if Settings.suspicion == true.

Where does Settings.suspicion become false?

1 Like

bruh, im stupid man. thank you for pointing that out.

1 Like

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