Looking for input on my mob movement script. New scripter

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.

2 Likes

Nice! For only two months of scripting, you’ve clearly learned a lot. Even after like a year and a half of scripting for me, I don’t think I could create this!

This isn’t very important but I should just mention that task.wait should be used into favor of the regular wait

2 Likes

Thanks, I watched a lot of videos lol.

Is task.wait something that is preferred in every situation you would use wait?
Or is it just when we put a wait in a loop or a repeating sequence of sorts?

3 Likes

It’s kinda just used overall to replace wait. The reason wait isn’t deprecated yet is because it’ll make it harder for beginners to find I’m pretty sure

2 Likes

Nice thanks. Appreciate the feedback. I will use task.wait from now on.

2 Likes

So Im still hoping to find a solution. So i did realize the coroutine is in bad practice. Im not looking to run the coroutine over the main thread. I want the main thread to stop but I want the code in the coroutine to happen all at once. Then when its done continue on 2ith the main thread. Any help is greatly appreciated.

1 Like