Using non-humanoid NPC’s, I tween the rig to the value of ServerCFrame, just wondering if there is anything else I can tweak to make it more efficient as this is a vital part of my game.
RunService Controller:
local mobs = collectionService:GetTagged('mobs')
local FRAME_LIMIT_MOBS = 1/30
local FRAME_LIMIT_PROJ = 1/30
local projectilesTbl = {}
local mobsTbl: {Instance} = {}
local render = nil
local t: number = 0
local function callRender()
render = runService.PreRender:Connect(function(dt)
if t > FRAME_LIMIT_MOBS then
for pos: number = 1, #mobsTbl do
mobModule:render(mobsTbl[pos], dt)
end
t = 0
else
t += dt
end
if #mobsTbl == 0 then
render:Disconnect()
render = nil
end
end)
end
for _, v in pairs(mobs) do
newMob(v)
end
collectionService:GetInstanceAddedSignal('mobs'):Connect(newMob)
collectionService:GetInstanceRemovedSignal('mobs'):Connect(function(mob)
local find = table.find(mobsTbl, mob)
if find then
table.remove(mobsTbl, find)
end
mobModule:removeCachedMob(mob)
end)
Mob Module:
local FRAME_LIMIT_MOBS = 1/30
local TWEEN_STYLE_MOBS = TweenInfo.new(FRAME_LIMIT_MOBS*3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
local mobs = {}
local mobCache: {Instance} = {}
function mobs:removeCachedMob(mob)
if mobCache[mob] then
mobCache[mob] = nil
end
end
local function isValidMob(mob)
if not mobCache[mob] then
mobCache[mob] = {
HumanoidRootPart = mob:FindFirstChild("HumanoidRootPart"),
ServerPart = mob:FindFirstChild('ServerPart'),
Health = mob:FindFirstChild('Health'),
ServerCFrame = mob:FindFirstChild('ServerCFrame')
}
end
local cache = mobCache[mob]
return mob and mob.Parent and cache.HumanoidRootPart and
cache.ServerPart and cache.Health and cache.Health.Value > 0 and
cache.HumanoidRootPart.CFrame ~= cache.ServerCFrame.Value
end
local function updateMobPosition(mob)
tweenService:Create(
mobCache[mob].HumanoidRootPart,
TWEEN_STYLE_MOBS,
{CFrame = mobCache[mob].ServerCFrame.Value}
):Play()
end
function mobs:render(mob, dt)
if isValidMob(mob) then
updateMobPosition(mob)
end
end