Moving part script not working

Hello, I am a complete beginner scripter and was just experimenting around with making a moving part. I want the part to move 10 studs to the right, then 10 studs to the left, then stop. Could you tell me why this is not working?

local part = Instance.new(“Part”)

part.Position = Vector3.new(0,1,0)

part.CanCollide = false

part.Anchored = true

part.Parent = workspace

while part.Position.X ~= 10 do

part.Position = Vector3.new(0.1,0,0)

wait()

end

while part.Position.X ~= -10 do

part.Position = Vector3.new(-0.1,0,0)

wait()

end

1 Like

you set the anchored property to true set it to false then it should work

also why are you running it twice

The second time the value is negative. Also, when I unanchored the part, it dissapeared.

re anchor it and change its CFrame Instead and use CFrame.new

or else you would have to use MoveTo

I modified the code as follows:

local part = Instance.new(“Part”)

part.Position = Vector3.new(0,2,0)

part.CanCollide = false

part.Anchored = true

part.Parent = workspace

while part.Position.X ~= 10 do

part.CFrame = part.CFrame + Vector3.new(0.1,0,0)

wait()

end

while part.Position.X ~= -10 do

part.CFrame = part.CFrame + Vector3.new(-0.1,0,0)

wait()

end

Now the part keeps going.

keeps going what?

i do not understand

Like when the X value reaches 10, it keeps going.

reset its cframe back then try again

What do you mean, reset it back to zero?

reset its original Cframe like before

local part = Instance.new(“Part”)
part.Position = Vector3.new(0,2,0)
part.CanCollide = false
part.Anchored = true
part.Parent = workspace

while true do
part.CFrame = part.CFrame + Vector3.new(0.1,0,0)
wait()
	if part.Position.X ~= 10 then
	
    CFrame.new(0,0,0)

end

end

while true do
part.CFrame = part.CFrame + Vector3.new(-0.1,0,0)
wait()
if part.Position.X ~= -10 then

    CFrame.new(0,0,0)

end

end

Why is this not working?

Why it’s not working:

I never use Position but it should be something like this when you use CFrame:
part.CFrame = CFrame + Vector3.new(0,2,0)

You aren’t using CFrame properly, I’d fix it something like this:
part.CFrame = CFrame + Vector3.new(0.1,2,0)

What is X? I’m pretty sure that’s not a property.

You are not being specific on what you want to change. Replace CFrame.new(0,0,0) with part.CFrame = CFrame.new(0,2,0) 2 because that’s the height of the part.

The same with the rest of the code.

Therefore, I will use TweenService for smoother movement than CFrame.

local part = Instance.new("Part") -- Create part
part.Parent = workspace
part.Anchored = true
part.CanCollide = true
part.CFrame = CFrame.new(0,5,0) -- Spawn Position.

wait(10) -- A wait function so it gives you time to load.
local tweenService = game:GetService('TweenService') -- Get TweenService.
local tweenInfo = TweenInfo.new(3) --Time to reach to final position.
local properties = {CFrame = CFrame.new(10,5,0)} -- The new position you want.
local tween = tweenService:Create(part, tweenInfo, properties) --Creating tween
tween:Play()

wait(1)
-- The same as the last line but makes it turn back
local tweenService = game:GetService('TweenService')
local tweenInfo = TweenInfo.new(3)
local properties = {CFrame = CFrame.new(0,5,0)}
local tween = tweenService:Create(part, tweenInfo, properties)
tween:Play()

Before pasting the entire code you should learn about CFrame for the first answer and TweenService for the fixed code.
Check it twice so you understand it. Hope it works! :+1:

Please correct me if I am wrong, as I am half new in scripting and there may be other ways to do this.

2 Likes