Help with identifying what makes my game lag

Hello everyone,

I’ve been trying to use some behavior trees on my zombies. So far, they work, but they’ve been very laggy. Digging deeper, I realized it was the thing in red:

However, I don’t really know what this means. Is it lagging because I’m using :FindFirstChild() too much or something else that I don’t know?

Here are the scripts (that are relevant):

Script_BTree (where behavior trees are run on)
local RUS = game:GetService("RunService")
local SES = game:GetService("ServerStorage")

local BTrees = SES:WaitForChild("BehaviorTrees")
local Creator = require(BTrees.BehaviorTreeCreator)
local ZombieTree = Creator:Create(BTrees.ZombieTree)


game.ReplicatedStorage.teleport.OnServerEvent:Connect(function()
	
	task.wait(2)
	
	local update = nil
	local frame = 1
	update = RUS.Heartbeat:Connect(function()
		for i, v in pairs(workspace.Zombies:GetChildren()) do
			local hum = v:FindFirstChildOfClass("Humanoid")
			local status = ZombieTree:run{v, frame, hum}
		end
		frame = frame + 1
		if frame > 99 then
			frame = 1
		end
	end)
end)
checking health
local Blackboard = obj.Blackboard
	local Zomb = obj[1]
	local Hum = obj[3]
	if Zomb:GetAttribute("ZombHealth") > 1 then return SUCCESS end
	
	print("killing")
	Hum:TakeDamage(100)
	Zomb:SetAttribute("Dead", true)
	Zomb.Parent = game.ServerStorage.RespawnZombies
	Zomb.PrimaryPart.Anchored = true
	
	delay(1, function()
		Zomb:SetAttribute("Dead", false)
		Zomb.Parent = workspace.Zombies
		Hum.Health = 100
		Zomb:SetAttribute("ZombHealth", 100)
		Zomb.PrimaryPart.Anchored = false
	end)
	return FAIL
checking close players
local Blackboard = obj.Blackboard
	local Zomb = obj[1]
	local result = 2
	
	for i, v in pairs(PLR:GetPlayers()) do
		local c = v.Character
		if not c then return end
		if (c.PrimaryPart.Position - Zomb.PrimaryPart.Position).Magnitude < 200 then
			result = 1
			obj.Blackboard.Target = c.PrimaryPart
		end
	end
	
	
	return result
attacking player
local Blackboard = obj.Blackboard
	local Zomb = obj[1]
	if (Blackboard.Target.Position - Zomb.PrimaryPart.Position).Magnitude < 4 then
		local TargetHum = Blackboard.Target.Parent:FindFirstChildOfClass("Humanoid")
		if not TargetHum or obj[2]%30 > 0 then return FAIL end
		print(obj[2]%30)
		TargetHum:TakeDamage(20)
	end
	
	return SUCCESS

Thank you for your time helping.

2 Likes

I haven’t looked through all your scripts yet, but right away in the first one I see this:

	update = RUS.Heartbeat:Connect(function()
		for i, v in pairs(workspace.Zombies:GetChildren()) do
			local hum = v:FindFirstChildOfClass("Humanoid")
			local status = ZombieTree:run{v, frame, hum}
		end

Running GetChildren() through heartbeat can definitely hurt as it’s constantly creating new tables and searching for children. If possible, I would instead have an empty table outside of the function called Zombies and just remove and add new zombies every time one is added. Then just loop through the Zombies table

1 Like

Thanks for you suggestion, I’ll definitely add that to my code. After adding some labels to the scripts, I found this:

unknown-1

I know why there are multiple “health checks” due to the delay() I used in my script but other than that, I have no idea why the checks take super long.

1 Like

My apologies, I know this is completely unrelated, but could you tell me what Behaviour trees you are using? And is there a guide on how to use them? Thanks and apologies.

1 Like

They were called BTrees

i actually saw your comment on the bottom asking to find a tutorial. Just go to the github link they have (https://github.com/Defaultio/BehaviorTree3) then copy and paste the code of each one into seperate module scripts. Make sure the TreeCreator Module script is the parent of the behavior tree.

Then, require the treecreator module, use its create(-- the reference to the folder that the editor makes goes here --) method, get its return value, then call the run method.

local TreeCreator = require(script.TreeCreator)
local TestTree = TreeCreator:Create(script.TestTree)

TestTree:run{"hi"}
1 Like