There are numerous ways to move a part or model in Roblox.
Parts
For parts, the easiest way to keep its rotation and orientation while changing its position is by changing its Position property (I made the position change complicated on purpose):
script.Parent.Touched:Connect(function(hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) then
script.Parent.Position = Vector3.new((script.Parent.Position.X + hit.Parent.HumanoidRootPart.X), (script.Parent.Position.Y + hit.Parent.HumanoidRootPart.Y), (script.Parent.Position.Z + hit.Parent.HumanoidRootPart.Z))
end
end)
Using CFrame will also move the part to a given position, but if you assign it to another part's CFrame plus a Vector3, the part being moved will have the rotation and orientation of the part it is moving along while having a different position. Example of that:
local part = script.Parent
part.CFrame = workspace.Baseplate.CFrame + part.Position
Or, you can assign it to a new CFrame:
script.Parent.CFrame = CFrame.new(5, 5, 5)
Models
Models usually have two main ways for the scripter to move them:
MoveTo(). Unlike a Humanoid's MoveTo(), which makes it walk to the given Vector3. a model is moved to the given Vector3, just like changing a part's Position.
SetPrimaryPartCFrame(). Assuming that the model's PrimaryPart is not nil, this method
changes the CFrame of the PrimaryPart to the given CFrame.
Example of using MoveTo():
script.Parent.ClickDetector.MouseClick:Connect(function()
local model = workspace:FindFirstChild("TestModel")
local target = workspace:FindFirstChild("TargetModel")
if model and target then
model:MoveTo(target.Core.Position)
end
end)
Example of using SetPrimaryPartCFrame and making sure that the PrimaryPart is not nil:
local model = workspace:FindFirstChild("TestModel")
local target = workspace:FindFirstChild("TargetModel")
script.Parent.ClickDetector.MouseClick:Connect(function()
if model and target then
if not model.PrimaryPart and not target.PrimaryPart then
model.PrimaryPart = model.Core
target.PrimaryPart = target.Core
elseif model.PrimaryPart and target.PrimaryPart then
model:SetPrimaryPartCFrame(target.PrimaryPart.CFrame + target.PrimaryPart.Position)
end
end
end)
Well, that's all to it.
1 comment
If you are talking about moving a part to a specific area, you can use vector 3 or CFrame. I recommend CFrame as it is a more powerful tool.
https://developer.roblox.com/api-reference/datatype/CFrame
You can also use setprimarypartcframe if it is a model.