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.