#RunService.Heartbeat
1 messages · Page 1 of 1 (latest)
Like for example
RunService.Heartbeat:Connect(function(dt)
-- global players data update
EnemyManager._checkTimer = EnemyManager._checkTimer + dt
local heavy = false
if EnemyManager._checkTimer >= DEFAULT_SETTINGS.checkInterval then
EnemyManager._checkTimer = 0
heavy = true
-- cache player positions
EnemyManager._playersData = {}
for _, p in ipairs(Players:GetPlayers()) do
local phrp = p.Character and p.Character:FindFirstChild("HumanoidRootPart")
if phrp then table.insert(EnemyManager._playersData, { player = p, pos = phrp.Position }) end
end
end
-- update enemies
for _, enemy in ipairs(EnemyManager._enemies) do
enemy:update(dt, EnemyManager._playersData, heavy)
end
end)
But I think even with the line if EnemyManager._checkTimer >= DEFAULT_SETTINGS.checkInterval then
it still makes the connection every heartbeat, just with a check for time elapsed each connection?
what does the variable heavy do?
flag for enabling the heavy logic (checking player distance from the enemies)
okay you really need to name your variables better
ik man
but I need confirmation though
and i still dont get what the code does
and if it's true is there another way to check player distance from enemies without making a connection every frame?
whats the issue though
it's not optimized
I don't want my code to be checking distance of every player from every enemy every single frame
and I think a delay line in the heartbeat connection it doesn't delay it just adds a check and there's still a connection every frame
but you really shouldn't be making complicated optimizations before you're done implementing everything
how would I do that?
I'm done implementing everything
I see a noticeable drop in framerate with just 30 npcs spawned in