I can't find a workaround to lag

Hello,

I’ve been working on some NPCs that uses a BTree plugin (BehaviorTrees3 + BTrees Visual Editor v3.0). The NPCs use them according to their state.

    if self.State == STATES.Follow then
		self.CurrentStatus = FOLLOW_TREE:run({Npc = self}, dt)
	elseif self.State == STATES.Attack then
		self.CurrentStatus = ATTACK_TREE:run{Npc = self}
	elseif self.State == STATES.Idle then
		self.CurrentStatus = IDLE_TREE:run{Npc = self}
	end
	
	self:HandleStatus()

I run the BTrees and checks every Heartbeat on the server in order to run multiple NPCs on the same tree. This is because my NPCs need to yield a lot (ex: cooldown for attacking and checking target) and yielding in the specific node will yield the entire tree until it is executed meaning NPCs will not do the actions needed to complete.

    local Blackboard = obj.Blackboard
	if obj.Npc:GetCoolDown("Damage") then return SUCCESS end
	
	obj.Npc.TargetHum:TakeDamage(5)
	obj.Npc:SetCoolDown("Damage", 1)
	
	return SUCCESS

Though running it on heartbeat works, it is very laggy. Take a look at this microprofile dump:

image

As you can see, it takes 1.155 milliseconds to run 1 single which is quite long, most of it from the BTree (namecall_FindFirstChild). I really don’t know workarounds to the lag that will stop the tree from yielding. Are there any workarounds to this that I’m unaware of? Your help is greatly appreciated.