Problems with RunService.Heartbeat stopping instantly upon disconnection

I’m having issues with disconnecting a RunService.Heartbeat event as soon as a certain limit is reached, but it seems to fire a fairly large number of times after that after it’s supposed to have disconnected. Here’s my code (the relevant parts):

if StareMeter >= 100 then
StareRunserviceHeartbeatConnection:Disconnect()
print("Disconnected.")
CanStare = false
elseif StareMeter <= 0 then
CanFindPlayer = true	
end

StareMeter is a number variable.
CanFindPlayer is a bool, and CanStare is also a bool. This is being ran inside of a local function in a serverscript. The local function is connected to a RunService.Heartbeat event. If you need more code for context, let me know. If proposed solutions don’t work i’ll expose the full code so everybody can get a look at it and see if there’s anything wrong there.

3 Likes

Can you send the full code? That would be helpful

3 Likes

It’s alot, but okay. Here:

local Volatile = script.Parent
local RunService = game:GetService("RunService")
local PathfindingService = game:GetService("PathfindingService")
local TweenService = game:GetService("TweenService")

local VolatileHead = Volatile:FindFirstChild("Head")
local VolatileRightArm = Volatile:FindFirstChild("Right Arm")
local RightArmHitbox = VolatileRightArm:FindFirstChild("RightArmHitbox")
local VolatileLeftArm = Volatile:FindFirstChild("Left Arm")
local LeftArmHitbox  = VolatileLeftArm:FindFirstChild("LeftArmHitbox")

local EyeUI = VolatileHead:FindFirstChild("EyeUI")
local EyeImage = EyeUI:FindFirstChild("EyeImage")

local Humanoid = Volatile:FindFirstChildOfClass("Humanoid")
local Animator = Humanoid:FindFirstChildOfClass("Animator")

function getvolatilechildren()
	local childrentable = {}
	for i, v in pairs(Volatile:GetChildren()) do
		if v:IsA("Part") then
			table.insert(childrentable,v)
			return childrentable
		end
	end
end

local volatileidle = Volatile:FindFirstChild("volatileidle")
local volatileidletrack = Animator:LoadAnimation(volatileidle)
local volatileattack = Volatile:FindFirstChild("volatileattack")
local volatileattacktrack = Animator:LoadAnimation(volatileattack)
local rayignoretable = getvolatilechildren()

local Pursuing = false
local CanFindPlayer = true
local CanStare = false

local StareVictim = nil

local ViewDistance = 100
local StareMeter = 0.5

local EyeStareColor = Color3.new(1, 0.878431, 0.878431)
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)})

Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if Humanoid.MoveDirection.Magnitude == 0 then
		volatileidletrack.Looped = true
		volatileidletrack:Play()
	else
		volatileidletrack.Looped = false
		volatileidletrack:Stop()
	end
end)

function Stare()
	print("stare running..")
	if CanStare == true and StareVictim ~= nil then
		local StareVictimHumanoid = StareVictim:FindFirstChildOfClass("Humanoid")
		if StareVictimHumanoid then
			local StareRunserviceHeartbeatConnection
			local function StareProcedure()
			if CanStare == true then
			print("Heartbeating.")
      wait(0.5)
			workspace.Part.SurfaceGui.TextLabel.Text = StareMeter 
				if StareMeter >= 100 then
					StareRunserviceHeartbeatConnection:Disconnect()
					print("Disconnected.")
					CanStare = false
			 elseif StareMeter <= 0 then
				CanFindPlayer = true	
			 end
			local SightRay2 = Ray.new(VolatileHead.Position, VolatileHead.CFrame.LookVector * 50)
				local SightRayResult2 = workspace:FindPartOnRay(SightRay2, VolatileHead)	
				if SightRayResult2 then
					local SightRayResultParent = SightRayResult2.Parent
					if SightRayResultParent:IsA("Model") then
						local SightRayResultHumanoid = SightRayResultParent:FindFirstChildOfClass("Humanoid")
						if SightRayResultHumanoid then
							StareMeter += 0.5
							EyeStareColor:Lerp(Color3.new(255,0,0),0.5)
						else
							StareMeter -= 0.5
							EyeStareColor:Lerp(Color3.new(255,255,255),0.5)
						end
					end
					end
				end
			end
			StareRunserviceHeartbeatConnection = RunService.Heartbeat:Connect(StareProcedure)
		end
	end
end


function FindPursuitVictim()
	if CanFindPlayer == true then
	local SightRay = Ray.new(VolatileHead.Position, VolatileHead.CFrame.LookVector * 50)
	local SightRayHitPart, SightRayHitPos = workspace:FindPartOnRay(SightRay, VolatileHead)
	if SightRayHitPart then
		local StareVictimParent = SightRayHitPart.Parent
		if StareVictimParent and StareVictimParent:IsA("Model") then
			local StareVictimHumanoid = StareVictimParent:FindFirstChildOfClass("Humanoid")
				if StareVictimHumanoid then
			  CanStare = true
				StareVictim = StareVictimParent
				coroutine.resume(coroutine.create(Stare))
				CanFindPlayer = false
			end
		end
	end
end
end

RunService.Heartbeat:Connect(function()
	wait(1)
	FindPursuitVictim()
end)

That’s literally everything. Feel free to point out any other errors or hand out tips. I’ve been working on this for days now, any problem solutions or tips would be very appreciated.
(i hope nobody steals this)

2 Likes