local Player = game:GetService("Players")
local Part = script.Parent.Position
Player.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local hmrp = character:WaitForChild("HumanoidRootPart")
wait(3)
Part = hmrp.Position - Part
end)
end)
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
game.Players.PlayerAdded:Connct(function(plr)
plr.CharacterAdded:Connect(function(char)
local hrp = char:WaitForChild("HumanoidRootPart") --Wait for the RootPart
toMove.Position = hrp.Position --Make the part's position the same as the RootPart
end)
end)
local Player = game:GetService("Players")
local Part = script.Parent
Player.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
wait(3)
Part:PivotTo(character:GetPivot())
end)
end)
This should work :).
I edited the part to be only script.Parent not script.Parent.Position
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")
Player.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
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
end
wait(0.5)
end
end)
end)
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
--therefore
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???
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
end
end
end
movement(Part1.Position, Part2.Position)