Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to teleport a PART/PARTS to a specific area, not a player?

I am wishing to teleport parts, or even models, using the standard “touch a block, get teleported to this other block” method. But so far, everything revolves around teleporting players- not brick models. Is there a way to do this? Ideally, I would not want to recreate the parts where they’re getting teleported to; but rather the ACTUAL parts be moved to the position.

Any help is GREATLY appreciated.

Thank you so so much.

1 comment

0

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.

1 answer

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 -- Checks if the PrimaryParts of both models are nil
			model.PrimaryPart = model.Core
			target.PrimaryPart = target.Core
		elseif model.PrimaryPart and target.PrimaryPart then -- Checks if they are not nil
			model:SetPrimaryPartCFrame(target.PrimaryPart.CFrame + target.PrimaryPart.Position) -- This is a CFrame
		end
	end
end)

Well, that's all to it.

This discussion is preserved as it was. New answers, comments, and votes are closed.