Why does my script not get the target Position


how can my script get the target Position?

I can´t understand why my script doesnt work. Roblox math should make sense as in real life math.

But nothing happens!

Here is the code, really simple!:

                local Player = game:GetService("Players")
                local Part = script.Parent.Position
                		local hmrp = character:WaitForChild("HumanoidRootPart")
                		Part = hmrp.Position - Part

1 Like

Are you trying to get the distance between the player and a part?

1 Like

No, I am trying that the Part is going to the humanoid Position

In that case, something like this should work. I’m not really sure why all these maths need to be done when you can just use the player’s position.

local toMove = script.Parent.Position --Part that needs to be moved

      local hrp = char:WaitForChild("HumanoidRootPart") --Wait for the RootPart
      toMove.Position = hrp.Position --Make the part's position the same as the RootPart

Okey I am sorry but what is the guy trying to explain then??

local Player = game:GetService("Players")
local Part = script.Parent                

This should work :).

I edited the part to be only script.Parent not script.Parent.Position

What is the guy explaining in the video? Did he get it wrong because I learned it in school just like he did?

Imma trying to understand why my solution is actually different and why cant I subtract each components.

as you can see I can get to the Position

Hes creating a Vector2 for a 2D space, he is not using a Vector3 to move a part.

1 Like

You should probably not store it in local variable just do

Part.Position = hmrp.Position - Part.Position

@Odawg566 have a point. Just use what Roblox provides you new Pivots are amazing.

That makes no differences because he just could make the same routine with rationale R^3.

In the case of 2D he has to get points on the screen in the form of a Vector2 to move his sprite. You can raycast to the target and move along that position, which takes an origin and a direction. If you want to use his method your going to have to do a lot more work.

I don’t have in depth knowledge on other engines and how they operate but I know roblox has features that make this process easier than manually calculating the X,Y, and Z coordinates and moving a part to those.

Can you explain exactly or show me how it will look like with 3D because I had vectors in school and we did extremly easly with (look at the picture I sent in the comment section)

So why should I use raycast if I an just subtract each components?

The issue with this script is that while it gets the direction from the part going towards to the hmanoid root parts position.

--this will error
--hmrp.Position = Vector3 Position
--part is a BasePart Instance

It doesn’t actually modify the position of the part to go in the direction of the part.

Here is how it should go to move the part towards the humanoids position fully annotated.

local Player = game:GetService("Players")

		local partPet = Instance.new("Part")
		partPet.Size = Vector3.new(2,2,2)
		partPet.Anchored = true
		partPet.CanCollide = false
		partPet.Parent = workspace --create an anchored part
		local hmrp = character:WaitForChild("HumanoidRootPart")
		while true do--loop continuosly for this new part created
			--get the direction from part to humanoid root part
			local directionPartToHumanoidRootPart = hmrp.Position - partPet.Position
			--check if there is a direction to avoid NAN error for .Unit
			if directionPartToHumanoidRootPart.Magnitude > 0.01 then
				--get the direction with a stud length of one by using .Unit
				local unitMovementDirection = directionPartToHumanoidRootPart.Unit
				--add it with the part pets current position to make it move towards the character, 1 studs in length because we .Unit the vector
				partPet.Position = partPet.Position + unitMovementDirection

Can you explain me this part a bit better?

–check if there is a direction to avoid NAN error for .Unit
if directionPartToHumanoidRootPart.Magnitude > 0.01 then

Because I dont think I can visualize me why - without this line - the script could give an error?

Let’s say the part is directly ontop of the humanoid part position. They have the same Vector3 Position let’s say Vector3.new(5,5,5).

So when you do the math to find the direction from part to humanoid root part like what the guy in the video was saying although since we are using Roblox Vector3’s it does it for us automatically:

This will be

--The formula
local directionPartToHumanoidRootPart = hmrp.Position - partPet.Position

--consider both equal to Vector3.new(5,5,5)
local directionPartToHumanoidRootPart = Vector3.new(5,5,5) - Vector3.new(5,5,5)
--solving it
local directionPartToHumanoidRootPart = Vector3.new(0,0,0)
--There is no direction

The formula for Vector3.Unit, is to divide a vector by it’s own magnitude. so lets do that.

local directionPartToHumanoidRootPart = Vector3.new(0,0,0)
--not actual code just math equality
directionPartToHumanoidRootPart.Unit = Vector3.new(0,0,0)/Vector3.new(0,0,0).Magnitude
Vector3.new(0,0,0).Magnitude = 0 --vector has no length at 0,0,0
directionPartToHumanoidRootPart.Unit = Vector3.new(0,0,0)/0
--Divide by zero error NAN
--Output will be Vector3.new(NAN,NAN,NAN)
--Part will dissapear

So why is roblox not fixing this problem because like even ? I mean its like if I script I have also to look for bugs. Does that also mean there will always exist a bug in the game?

Sorry I am not so long into scripting but even “if directionPartToHumanoidRootPart.Magnitude > 0.01 then” could give a bug or am I wrong cuz 0.009 could catch it???

Why roblox doesn’t fix it:

It’s our job to do the math correctly.

As long as the magnitude is not equal to 0 or extremely close to zero it’s fine to .Unit it. 0.009 could work as well.

1 Like

Can you explain why my script is wrong I did the same thing as you did but still

local Part1 = game.Workspace:WaitForChild("Part1")
local Part2 = game.Workspace:WaitForChild("Part2")
local Part3 = game.Workspace:WaitForChild("Part3")

local function movement(V1, V2)
	while wait() do
	local direction = V1 - V2
		if direction.Magnitude > 0.01 then 
		local movementunit = direction.Unit
			V2 = V2 + movementunit

movement(Part1.Position, Part2.Position)

The issue is that you are not editing the properties of a part.

You are adding a vector onto the variable V2. But it’s not going to change the property of a part.

You will need to edit the property by setting the position property like below:

So I cant use a function for this you mean?