Moving a part back and forth

yeah im trying to move this object however it wont move please help me

while wait() do
	local a = 0
	while wait(0.1) do
		script.Parent.Position.X = script.Parent.Position.X + 1
		local a = a + 1
		if a == 26 then
			break
		end
		local a = 0
		while wait(0.1) do
			script.Parent.Position.X = script.Parent.Position.X - 1
			local a = a + 1
			if a == 26 then
				break
			end
		end
	end
end

please help with this

You can’t assign to β€˜β€˜x’’, here is a working code:

while wait() do
	local a = 0
	while wait(0.1) do
		script.Parent.Position = script.Parent.Position + Vector3.new(1, 0, 0)
		local a = a + 1
		if a == 26 then
			break
		end
		local a = 0
		while wait(0.1) do
			script.Parent.Position = script.Parent.Position - Vector3.new(1, 0, 0)
			local a = a + 1
			if a == 26 then
				break
			end
		end
	end
end
1 Like

@killr1292

Instead of doing a while loop and counting up, simply use a for loop.

while task.wait() do
	for a = 1,26,1 do
		task.wait(0.1)
		script.Parent.Position = script.Parent.Position + Vector3.new(1, 0, 0)
	end
	
	for a = 1,26,1 do
		task.wait(0.1)
		script.Parent.Position = script.Parent.Position + Vector3.new(-1, 0, 0)
	end
end

Also, if you want to make it simpler. You can use compound assignments. Such as, +=
You can find the different assignments here

More about for loops here

1 Like