Help with Formulas

I’ve been working on a Room Based mob system, and I’ve been trying to figure out how to make a formula to calculate mob spawn positions based on how many mobs are being spawned, and what the spawner’s position is.

Example:

Is there any way of doing this? Please including code examples, and or explanations on how it works!

1 Like

For this scenario,There are easy or harder ways to solve this case(harder coding will be better)

First,u can insert an int value to get the total mobs from

After that u average all the spawning points using a complicated code

E.g. ((position.Magnitude + Position.Magnitude)/ 2(amount of spawners u have)

If i want to do the hard one,It will invovle a lot of trigo and vectors to do it

1 Like

For this part, what would

position = ? -- Would this be the Spawner position
Position = ? -- Would this be Mob's Torso/HRP position
-- ANDDD
/ 2(amount of spawners u have) -- Would the '2' be replaced with the amount?

They are both spawners position

1 Like

What would the ‘hard’ way look like?

I need time for the hard wayIt is not simple.

1 Like

Alright, take your time! Thanks for helping.

By the way, the mob loading part of code is this:

function module.LoadMobs(Room)
	
	local function ClosestPlayer(mob)
		
		local currentDist = math.huge
		local currentTarget = nil
		
		for _, v in pairs(game.Players:GetPlayers()) do
			
			local distance = (mob.Torso.Position - v.Character.Torso.Position).Magnitude
			if distance < currentDist then
				
				currentDist = distance
				currentTarget = v.Character
				
			end
			
		end
		
		return currentTarget
		
	end
	
	for _, v in pairs(Room.Enemies:GetChildren()) do
		
		for i=1, v.Amount.Value do
			
			local mob = Mobs:FindFirstChild(v.Enemy.Value)
			if mob then
				
				mob = mob:Clone()
				mob.Parent = workspace.Mobs
				
				local posFormula = (v.Position + v.Position).Magnitude / v.Amount.Value
				
				mob:PivotTo(CFrame.new(posFormula))
				--mob:PivotTo(v.CFrame + Vector3.new(0, 0, (4 * i)))
				
				task.spawn(function()
					
					repeat wait()
						
						local target = ClosestPlayer(mob)
						if target ~= nil then
							
							if target:FindFirstChild("Torso") then
								
								mob.Humanoid:MoveTo(target.Torso.Position)
								
							end
							
						end
						
					until mob.Humanoid.Health < 1	
					
				end)
				
				mob.Humanoid.Died:Connect(function()
					
					task.wait()
					
					mob:Destroy()
					
				end)
				
			end
			
		end
		
	end
	
end

I would like to help, but honestly I dont understand what you mean… and the drawing didnt help too much… lol.

If there are 3 mobs already spawned, the position of the spawners should change? to where? or following what rule?

1 Like

So basically, if 3 mobs spawn at one spawner, they should all align at the spawner. (trying to simplify it!!)

“Align” at the spawner? like a queue, a mob behind the previous one?

1 Like

No, like, imagine there are 3 zombies that spawn at a spawner. They would all start at the spawner, the first zombie would spawn exactly at the spawner, the second would spawn 3 studs to the left of it, and the 3rd would spawn 3 studs to the right of it.

Example:

local Positions = {
    [1] = Vector3..new(0, 0, 0),
    [2] = Vector3.new(-3, 0, 0),
    [3] = Vector3.new(3, 0, 0),
}

Ok, I think I start to understand, like an army creating lines.
So the first zombie at the center of the spawner and the next ones with a 3 studs offset.

That would be pretty easy to achieve, if per spawner you have a reference to the last model (mob) created, then for the second mob to spawn you check that variable, get its position and add the 3 studs offset on its CFrame to the left. Then you set the variable to reference that new model (mob). Each new mob will spawn at the left of the previous with the 3 offset

1 Like

But heres the thing, I also need a formula to calculate the positioning for amount of mobs for 1-5+. And that would be pretty confusing to work out, as you would have to do a lot of math.

A previous formula I’ve tried, is this:

mob:PivotTo(v.CFrame + Vector3.new(0, 0, (4 * i)))

‘i’ is the current iteration of the for loop when loading the mobs.
‘v’ is the spawn point, aka a part.

This works well, but it is offset from the actual spawn point.

U have to use trigo for this.u show him how it is done,i think it would be easier for u to show the code

1 Like

Wait, why would we use trigo in this case?

Let amount of mobs as A, you would need to do

mob:PivotTo(v.CFrame + Vector3.new(0, 0, (4*i) - (A/2*4)))

To offset them so half of them is at the left and half of them is at the right

1 Like

What if the A is an odd number? Like 1, 3, 5, etc.

We also need to consider its orienattion.

1 Like

So, you mean, this wont work? what you mean with this?

Something like this wont need to calculate any math about their positions:

local mob = workspace.Model

local LastMob = false
local spawner = workspace.spawner

for c=1, 500 do -- 500 mobs all of them at the left of the last one
	local newMob = mob:Clone()
	if LastMob then
		newMob:PivotTo(LastMob.WorldPivot * CFrame.new(-3,0,0))
	else
		newMob:PivotTo(spawner.CFrame)
	end
	newMob.Parent = workspace
	LastMob = newMob
end
1 Like