Moving back and forth works but, distance doesn't work

  1. What do you want to achieve? Make player move back and forth , forwards and backwards at a certain distance.

  2. What is the issue? My calculations isn’t really good, and it doesn’t move back and forth at a distance, I put the variable for the distance to 0.01 for it to be as close as possible, but it still is the same distance.

  3. What solutions have you tried so far? I tried changing the code, and searching in devforum, didn’t find anything related.

local MoveSpeed = 100
local MoveDistance = 1
local MoveDirection = 1

local IsMovementEnabled = true

local function MoveBackAndForth()
    while task.wait(1/60) do
        if IsMovementEnabled then
            local NewPosition = HumanoidRootPart.CFrame.Position + Vector3.new(0, 0, MoveSpeed * MoveDirection)

            if NewPosition.Z > MoveDistance/2 or NewPosition.Z < -MoveDistance/2 then
                MoveDirection = -MoveDirection
            end

            HumanoidRootPart.CFrame = CFrame.new(NewPosition)
        end
    end
end

Have you called the MoveBackAndForth() function? The code you’ve provided doesn’t call the function.

I did try calling it, the code I sent was just a snippet;

“I put the variable for the distance to 0.01 for it to be as close as possible, but it still is the same distance.”

Whilst I’m here, let me suggest a few changes to your code

local RunService = game:GetService("RunService")

local MoveSpeed = 100
local MoveDistance = 1
local MoveDirection = Vector3.new(0,0,1) -- A number is not a direction. A vector is a direction.

local IsMovementEnabled = true

function MoveBackAndForth() -- No need to make the function local
    if not IsMovementEnabled then return end -- This reduces indenting by inverting the if statement
    local MoveOffset = MoveDirection * MoveSpeed -- Makes code easier to read
    local NewPosition = HumanoidRootPart.CFrame.Position + MoveOffset

    local OutOfBounds = NewPosition.Z > MoveDistance/2 or NewPosition.Z < -MoveDistance/2
    if OutOfBounds then -- Easier to read like this
        MoveDirection = -MoveDirection
    end
    HumanoidRootPart.CFrame = CFrame.new(NewPosition)
end

RunService.Heartbeat:Connect(MoveBackAndForth) -- This eliminates the need for that ugly task.wait(1/60). It calls MoveBackAndForth every physics frame.
1 Like

Can you try set the MoveSpeed to 1 and the Distance to 100?
At the moment, you’re calling the function every frame, and it’s travelling 100 studs each frame, with a max distance of 1 stud.

To make this easier, I recommend using the delta time variable.
The delta time variable represents the amount of seconds that have passed since the last physics frame. Since the amount of time can vary, this keeps the distance travelled constant.

By multiplying the MoveSpeed by the DeltaTime, we’re making the MoveSpeed the amount of studs travelled per second instead of per frame.

function MoveBackAndForth(DeltaTime)
    if not IsMovementEnabled then return end
    local MoveOffset = MoveDirection * MoveSpeed * DeltaTime
    local NewPosition = HumanoidRootPart.CFrame.Position + MoveOffset

    local OutOfBounds = NewPosition.Z > MoveDistance/2 or NewPosition.Z < -MoveDistance/2
    if OutOfBounds then
        MoveDirection = -MoveDirection
    end
    HumanoidRootPart.CFrame = CFrame.new(NewPosition)
end

Sorry for late reply, I tested it, the speed does have an affection to changing the distance but not that much. I put the speed to 1, and the distance to 100; the speed does a bit of something, but close to nothing, while the distance remains the same. It doesn’t change.