You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? i want a working moving platform that moves and comes back to its position
What is the issue?
it first go really far then after that it does the loop. how do i fix to only do the loop
local part = script.Parent
local dir = 1
local max_val = 25
local min_val = -25
local wait_time = 0.1
while true do
part.Position = part.Position + Vector3.new(1*dir,0,0)
wait(wait_time)
if (part.Position.X > max_val) then
dir = -1
end
if (part.Position.X < min_val) then
dir = 1
end
end
That’s because max_value and min_value aren’t relative to the part default position, therefore the part will only work as expected if it’s starting X axis is 0. Instead 25 should be the position offset:
local max_value = part.Position.X+25
local min_value = part.Position.X-25
Also there’s no need to overcomplicate your code, TweenService can help you smoothly move the part while reducing any lag that may be caused due to the loop and it’s much easier to implement with useful configurations:
local TweenService = game:GetService("TweenService")
local part = script.Parent
--tween configuration
local info = TweenInfo.new(
1, --time before the tween finishes
Enum.EasingStyle.Linear, --Linear easing style remains constant
Enum.EasingDirection.InOut, --doesn't make a difference because we're using linear
-1, --amount of repeats, -1 makes the repeats infinite
true --reverses
)
local offset = 25 --part offset
part.Position -= Vector3.new(offset, 0, 0)
--creating the tween, offset*2 because we subtracted the offset before
local tween = TweenService:Create(part, info, {Position = Vector3.new(part.Position.X+offset*2, part.Position.Y, part.Position.Z)})
tween:Play() --playing the tween
local part = script.Parent
local part_x_position = part.Position.X
local dir = 1
local wait_time = 0.1
while true do
part.Position += Vector3.new(1*dir, 0, 0)
task.wait(wait_time)
if (part.Position.X > part_x_position + 25) then
dir = -1
end
if (part.Position.X < part_x_position - 25) then
dir = 1
end
end