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

How to move a part using a script?

I want to make a moving spotlight. Please tell me how to move and rotate a part using a script.

1 comment

0

thank you everyone who helped me!

3 answers

✓ Accepted answer

I would suggest you to Google Basic Stuff, to understand them Better.

Firstly, You need to locate it, Your Part must be in the workspace. So you locate it with:

game.workspace.Part.CFrame = CFrame.new(4, 6, 5)

On The Parameters CFrame.new(You write here where you want the place to teleport to)

Hello, mr_maker231. Looks like your a beginner. You'd need to use Position. For example,

workspace.Part.Position = Vector3.new(10, 10, 10)

We use Vector3 for things like position and size. That code would make the part go up 10 studs on the Y-axis (hight), 10 studs on the X-axis (width), and 10 studs on the Z-axis (length). Studs are the units used to measure size and coordinates on Roblox. You can also use CFrames, but that can be a little too advanced for a beginner. Please accept this answer if it helped.

Hey there, by moving spotlight I assume you're trying to make a moving lamp or something similar. There are multiple ways to do this, and I'll start with the simplest method.

1. Incremental Vector3

This is fairly simple. Your brick has a Position and you can tell it to gradually move in a direction or a vector. To do this, you can use Vector3.

local Part = game.Workspace.Part
Part.Position = Part.Position + Vector3.new(0, 1, 0)

-- Here, we are telling the game that the part's position is now equal to itself... PLUS 1 stud in the y-axis.
-- What this means is that we are moving the part 1 stud upwards.

If we wanted to make the brick look like it's levitating up and down, we can use a loop:

local Part = game.Workspace.Part

while true do
	for i=1, 5 do
		Part.Position = Part.Position + Vector3.new(0, 0.1, 0)
		wait(0.1)
	end
	for i=1, 5 do
		Part.Position = Part.Position + Vector3.new(0, -0.1, 0)
		wait(0.1)
	end
end

2. Incremental CFrame

If you want to rotate and move the part then you would use CFrame. Unlike Vector3, a part's CFrame (from Coordinate-Frame) also contains values on the orientation of the part. If you only give it information on which direction to move the part, it will assume the new orientation is zero.

Part.CFrame = CFrame.new(script.Parent.Position + Vector3.new(0, 1, 0))

-- It's best to use CFrame when you need a rotation. If my part was rotated in any way before this script, it will rotate back to (0, 0, 0) afterwards because I haven't given it any orientation value.

3. TweenService

This is probably a bit more complicated than the previous two, but it's definitely more pleasing to the eye and I recommend taking the time to properly learn about TweenService.

Here's how I would set it up for a brick that 'swings' left and right.

local TweenService = game:GetService("TweenService")
local Part = game.Workspace.Part

local TweeningInformation = TweenInfo.new(
	2, -- The animation would last two seconds
	Enum.EasingStyle.Quad, -- Move the brick using a quadratic formula
	Enum.EasingDirection.Out, -- Move the brick outwards
	0, -- Repeat it zero times
	false, -- We don't want to reverse
	0.1 -- Let's have a tiny delay between every swing
)

local Destination1 = {Position = Part.Position + Vector3.new(2.5, 0, 0)} -- We're swinging 2.5 studs to the right.
local Destination2 = {Position = Part.Position + Vector3.new(-2.5, 0, 0)} -- ... And 2.5 studs to the left.

Tween1 = TweenService:Create(Part, TweeningInformation, Destination1) -- Defining the right-swing
Tween2 = TweenService:Create(Part, TweeningInformation, Destination2) -- Defining the left-swing

while true do -- Our main loop
	Tween1:Play() -- Play the swing-right animation
	wait(2.1) -- Wait the time it takes to finish
	Tween2:Play() -- Now to the left...
	wait(2.1) -- And wait for it to finish again
end

You'll notice I had to use Vector3 earlier to define the destination. CFrame still works fine, but I didn't want to reset the part's orientation to (0, 0, 0). If I wanted the lamp to tilt then I would have to use CFrame.

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