Sending remote event in a Heartbeat with out overloading the Events

I have this Heartbeat command that moves an NPC to a player and detects when its close enough to attack. However i need to send some kind of signal to a local script to play an animation when this happens. The problem is if i just put a remote command in it over loads and gives me the “Remote function invocation queue exhausted” error. Anyone know a way around it?

RunService.Heartbeat:Connect(function() 
	local nearestPlayer, distance, direction = FindNearestPlayer()

	if nearestPlayer then

		if distance <= TargetDistance and distance >= StopDistance then
			NPCHumanoid:Move(direction)
		else
			NPCHumanoid:Move(Vector3.new())
		end

		if distance <= AttackDistance and tick() - LastAttack >= AttackCooldown and NPCModel:GetAttribute("Stunned") == false then
			--runs when attacking
			LastAttack = tick()


1 Like

Why would you even wanna run this in a heartbeat? Just run it in a normal script.

1 Like

i need to local script to be detecting that the player is in range from the server script so i assumed that i would update the local script in the heartbeat. this specific script also crashes when i use something like a while true

Why not do it just on the server, instead of doing it on the client. While true do always crashes thats why u always put a task.wait() function so it does not.

I don’t even understand why you even wanna run that code in the heartbeat, secondly you can just play the animation on the server.

Well i want to use Animation events but they don’t work server side for some reason

I’m confused.

  1. Remote function invocation queue exhausted only happens if the request has timed out for a RemoteFunction, not a RemoteEvent, by not hooking a listener.
  2. Your code does not even show where it is firing the RemoteEvent. However, you can manually rate limit the RemoteEvent itself.
  3. You can play animations server-side - you’re probably doing it wrong.

The problem is not playing animations that works fine but i need to get animations events which dont work

This is my original code problem is i want to use the animation event in the AttackTrack to make some warnings appear and stuff. but using getAnimationEvents dont wark on server scripts according to the documentation

function FindNearestPlayer()
	local playerList = Players:GetPlayers()
	
	local nearestPlayer = nil
	local distance = nil
	local direction = nil
	
	for _, player in pairs(playerList) do
		local character = player.Character
		local distanceVector = (player.Character.HumanoidRootPart.Position - root.Position)
		if character then	
			if not nearestPlayer then 
				nearestPlayer = player
				distance = distanceVector.Magnitude
				direction = distanceVector.Unit

			elseif distanceVector.Magnitude < distance then
				nearestPlayer = player
				distance = distanceVector.Magnitude
				direction = distanceVector.Unit
			end
		end
	end
	return nearestPlayer, distance, direction
	
end
	

RunService.Heartbeat:Connect(function() 
	local nearestPlayer, distance, direction = FindNearestPlayer()
	
	if nearestPlayer then

		if distance <= TargetDistance and distance >= StopDistance then
			--Runs when walking
			if not WalkingTrack.IsPlaying then
				WalkingTrack:Play()
			end
			humanoid:Move(direction)
		else
			--Runs when standing still
			if WalkingTrack.IsPlaying then
				WalkingTrack:Stop()
			end
			humanoid:Move(Vector3.new())
		end
		
		if distance <= AttackDistance and tick() - LastAttack >= AttackCooldown and Model:GetAttribute("Stunned") == false then
			--runs when attacking
			
			AttackTrack:Play()
			LastAttack = tick()
			
			
			
			
			
			
			
		end
	end 
end)

-- what the walkspeed and jump height will revert to
local BaseWalkSpeed = 12
local BaseJumpHeight = 50

coroutine.resume(coroutine.create(function()
	while true do
		wait()
		if script.Parent.Parent:GetAttribute("Stunned") == true then
			script.Parent.Parent:FindFirstChild("Humanoid").WalkSpeed = 0
			script.Parent.Parent:FindFirstChild("Humanoid").JumpHeight = 0
		else
			script.Parent.Parent:FindFirstChild("Humanoid").WalkSpeed = BaseWalkSpeed
			script.Parent.Parent:FindFirstChild("Humanoid").JumpHeight = BaseJumpHeight
		end
	end
end))

Change the server script’s runcontext to client, that may help though im not so familiar with runcontext

Nvm dont do that its not going to help

I’ve been looking around the forum and asking questions and won’t this type of script need like module scripts or sm? I’m not really sure how to use them but they work as like. server and client side connection or something right?

Don’t worry about runcontexts they just expand where you can place your scripts, for example usually if you place a localscript into workspace it wont work but with runcontext it does.

As for the main issue, id recommend using a while task.wati(2) loop instead of using heartbeat and then firing the remote event when you need to.

when i do that the remote doesn’t even fire

sorry, but local script to play an animation on the npc??? that doesn’t even work??

am i missing something?

1 Like

every forum post i see regarding animation always state that its better to play animations on a local script to reduce lag and allow use of GetAnimationEvents like Animations, server or client?. so i just assumed that was the best thing to do.

yes but local scripts can only be used inside players, tools, guis, and i forgot if there’s anything else

if what you’re trying to animate is a NPC then you literally HAVE to use server scripts

I got a response on another thread talking about making animations events manually using keyframe timers so that solves that problem but I’m not sure about the fact about playing NPC’s animations client side to save performance

Here is the thread where a got this answer