I need a help with math and Vector3

in the screenshot that I will attach, it will be clear that according to my idea, the dummies should move towards me while maintaining a distance between themselves, however, they all move towards me, accumulating at one point.

To solve the problem, I tried to add a coefficient that would set the distance between me and the dummies, I also I tried to experiment with math (I hardly understand it), but it didn’t work out for me

		for i, Soldier in pairs(Army:GetChildren()) do
			local Pos = plr.Character.HumanoidRootPart.Position
			
			local SoldierPos = Soldier.HumanoidRootPart.Position
			local X = SoldierPos.X
			local Y = SoldierPos.Y
			local Z = SoldierPos.Z
			
			Soldier.Humanoid:MoveTo(Vector3.new(Pos.X + k, Pos.Y, Pos.Z + k))
		end
Soldier.Humanoid:MoveTo(Vector3.new(Pos.X + k, Pos.Y, Pos.Z + k))

Where are you getting k from?

1 Like

Does this give the intended result?

local minDistFromPlayer: number = --?
local minDistBetweenGoalPositions: number = --?

local function setGoalPositions(): ()
	local soldiers: {Model} = Army:GetChildren()
	local soldierPosSum: Vector3 = Vector3.zero
	for i: number, soldier: Model in soldiers do
		soldierPosSum += soldier.HumanoidRootPart.Position
	end
	local soldierAveragePosCurrently: Vector3 = soldierPosSum / #soldiers
	for i: number, soldier: Model in soldiers do
		local localSpacePosOffset: Vector3 = Vector3.new((-1 + (i - 1) % 3) * minDistBetweenGoalPositions, 0, -minDistFromPlayer - ((i - 1) // 3) * minDistBetweenGoalPositions)
		local playerHrpPos: Vector3 = plr.Character.HumanoidRootPart.Position
		local transfromCf: CFrame = CFrame.lookAt(playerHrpPos, Vector3.new(soldierAveragePosCurrently.X, playerHrpPos.Y, soldierAveragePosCurrently.Z))
		local goalPos: Vector3 = transformCf * localSpacePosOffset
		
		soldier.Humanoid:MoveTo(goalPos)
	end
end

// is floor division operator by the way. So (i - 1) // 3 is the same as math.floor((i - 1) / 3)

Edit: I originally wrote the code such that the goal positions depend on HumanoidRootPart orientation. Now it instead depends on which direction the soldiers are coming from.

2 Likes

Wow I didn’t know // was a thing. TIL. Is that specific to luau?

k - coefficient

local k = 4

I added it so that there is a space between the player and the soldiers

Wow, your code really surprised me and now everything works, I would like to know more about the code, so could you answer these questions for me or provide me with some kind of documentation?

local soldiers: {Model} --: {Model}  how does a variable work with ":" and {Model}?

and

#soldiers -- why and in what cases is # added?

Type annotations
: {Model} is a type annotation (it tells that the variable contains an array of Model instances. Type annotations are a completely optional Luau feature. They don’t affect the behavior of the code. However, they can be useful when writing code because

  • If you have defined the types of parameters of a function, the script editor can show those types when you are writing code where you call the function.
  • If you enable strict type-checking by writing --!strict at the top of your script, the editor will give you warnings if it detects you writing code that has type contradictions. Here’s an example.
--!strict
local someVariable: boolean = false
someVariable = 2

The editor would underline the line someVariable = 2 because it’s a number and the variable is supposed to hold a boolean.

However, as mentioned, they don’t affect the behavior of the code. The above code will run run without errors or warnings. The warnings are only present in the script editor.

Here’s a link for more information.

Array length operator
The # operator gives the length of an array (number of values in the array). For example, assuming that the nine soldiers in the image are all the soldiers whose parent is Army, #soldiers is 9.

If your table is a dictionary i.e. its indices are not consecutive integers starting from 1, then # gives 0 unless the same table also has an array part in which case the number of values in the array part is given.

Reply to @Uhsleep91
I don’t know which other languages have a floor division operator. Lua doesn’t have it. Python apparently does but idk which others do. However, in C# for example, division of positive integers of an integer datatype gives the same result.

2 Likes

Thanks, I understand how the code works and how I should have fixed my code, so I have no more questions. I really learned new things

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.