Help with timer that uses tick() that isn't working

Basically, i have a timer in my AI server script inside of a coroutine that isn’t working. It’s supposed to fire the PursuitEvent, but it doesn’t, and it doesn’t print “time’s up” either. Here’s the script, removed some parts that were irrelevant:

local PursuitEvent = Volatile:FindFirstChild("Pursuit")

local PursuitVictim = nil
local StareVictim = nil

local IsInPursuit = false
local IsStaring = false

local ViewDistance = 50
local CurrentTime = tick()
local StareTime = 2

local EyeStareColor = Color3.new(255,0,0)
local EyeTweenInfo = TweenInfo.new(0.5,Enum.EasingStyle.Linear,Enum.EasingDirection.In)
local EyeTween = TweenService:Create(EyeImage,EyeTweenInfo,{ImageColor3 = EyeStareColor})
local EyeTweenReverse = TweenService:Create(EyeImage,EyeTweenInfo,{ImageColor3 = Color3.new(255,255,255)})

function Staring()
	local eventconnectionrunservice
	local StareTimeSum = CurrentTime + StareTime
	eventconnectionrunservice = RunService.Heartbeat:Connect(function()
		if CurrentTime >= StareTimeSum and StareVictim ~= nil then
			print("times up")
			PursuitEvent:Fire()
		else eventconnectionrunservice:Disconnect() return end
	end)
end

local lastraypart 
local PlayerFound = false
function FindPlayer()
	if lastraypart ~= nil then
		lastraypart:Destroy()
	end
	local HeadRaycast = Ray.new(VolatileHead.Position,VolatileHead.CFrame.LookVector * ViewDistance)
	--[[local visualraycastpart = Instance.new("Part")
	local midpoint = HeadRaycast.Origin + HeadRaycast.Direction/2
	visualraycastpart.Parent = workspace
	visualraycastpart.CFrame = CFrame.lookAt(midpoint, HeadRaycast.Origin)
	visualraycastpart.Size = Vector3.new(0.5,0.5,HeadRaycast.Direction.Magnitude)
	visualraycastpart.Anchored = true
	visualraycastpart.CanCollide = false
	visualraycastpart.Transparency = 0.85
	lastraypart = visualraycastpart--]]
	local PlayerObject, HitPosition = workspace:FindPartOnRay(HeadRaycast,VolatileHead)
	if PlayerObject ~= nil and StareVictim == nil and PlayerFound == false then
			local PlayerObjectHumanoid = PlayerObject.Parent:FindFirstChildOfClass("Humanoid")
			if PlayerObjectHumanoid and PlayerObjectHumanoid ~= Humanoid then
			IsStaring = true
			EyeTween:Play()
			EyeTweenReverse:Cancel()
			StareVictim = PlayerObject.Parent
			PlayerFound = true
			coroutine.resume(coroutine.create(Staring))
			print(PlayerFound)
	  end
		end
	if PlayerObject == nil then
		PlayerFound = false
		print(PlayerFound)	
		StareVictim = nil
		PursuitVictim = nil
		EyeTween:Cancel()
		EyeTweenReverse:Play()
		coroutine.resume(coroutine.create(Staring))
	end
end

while wait(0.5) do
FindPlayer()
end

Let me know if there are any missing variables. Help will be much appreciated, as this is a rather picky method.

try use os.time() bc tick() mean time that passed ithink

1 Like

Replaced tick() with os.time(), same result, nothing happened.

Have you checked where the code stops using either print statements or break points? Does the print(PlayerFound) code run?

1 Like

Oh no, the FindPlayer() function runs fine and print(PlayerFound) runs fine. It’s the timer in the Staring() function that doesn’t work. “heartbeat event running” prints too, it’s just the if tick >= StareTimeSum statement that primarily doesn’t seem to run.

I resolved the issue. It was the fact that once the raycast fired, it would not fire again, and it would check bools that rendered the Staring event unable to fire. You should be able to see the error if you look closely. But, there was no issue with the timer. Thank you everybody for your help.