I’m working on a mob system, and I’m hoping to get some feedback on my code. I’ve been studying scripting for about 2 months and this is one of my first.
This script is for when the mobs are idle. The bricks that are temporarily visible are “ToWorldSpace” points so they don’t get too far away from the origin point.
Mainly what I’d like to know is…
- Is my script going to have good performance when there’s over 100 mobs in my game.
- Is the coroutine in good practice in this situation?
- Any suggestions from a more seasoned scripter for ways to improve
The problem I’m facing is the mobs don’t all start moving at the same time. As for the coroutine I commented out, if I uncomment it, it fixes the problem but then the mobs all move at the exact same time,
which is even more undesirable.
Here’s the code.
local mobs = game.Workspace.Mobs
local angryCubes = mobs.Mob1:GetChildren("AngryCube")
local function mobMoving(mob)
if mob.Parent == mobs.Mob1 then
local originPoint = mob:WaitForChild("Target")
local newPosition = nil
local randomX = nil
local randomZ = nil
local goalDestination = nil
randomX = math.random(-50,50)
randomZ = math.random(-50,50)
goalDestination = originPoint.CFrame:ToWorldSpace(CFrame.new(randomX,1,randomZ))
local speed = 25;
mob.CFrame = CFrame.lookAt(mob.Position, goalDestination.Position)
mob.Velocity = (goalDestination.Position - mob.Position).Unit*speed
local moving = nil
--local newThread = coroutine.create(function()
for i = 0, 15, 1 do
mob.CFrame += mob.CFrame.lookVector
wait(1/speed) -- speed here
end
--end)
--coroutine.resume(newThread)
end
end
while true do
wait(2)
local crt = coroutine.create(function()
for i, mob in pairs(angryCubes) do
wait(math.random(0.01,0.5))
mobMoving(mob)
end
end)
coroutine.resume(crt)
end
And here’s a short video clip, coroutine is commented out.
Sorry about the Movavi banner.
Any input is greatly appreciated as I’m still new to this and I feel dev forums might be the one place to help me level up my coding skills. Thanks.