Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- LocalScript placed in StarterPlayerScripts or StarterCharacterScripts
- local player = game.Players.LocalPlayer
- local character = player.Character or player.CharacterAdded:Wait()
- local humanoid = character:WaitForChild("Humanoid")
- local runService = game:GetService("RunService")
- local moveDirection = 1 -- 1 for forward, -1 for backward
- local moveInterval = 0.32 -- Interval in seconds before switching direction
- local lastSwitchTime = 0
- local isMoving = false
- local movementDuration = 1.6 -- Total duration for moving back and forth (adjustable)
- local waitDuration = 60 -- Wait time in seconds before each movement cycle
- -- Function to move the character forward and backward
- local function moveBackAndForth(deltaTime)
- if isMoving then
- -- Move forward or backward
- local direction = Vector3.new(0, 0, moveDirection * 0.16).Unit -- Reduce movement speed
- humanoid:Move(direction, true)
- -- Switch direction after the interval
- if tick() - lastSwitchTime >= moveInterval then
- moveDirection = -moveDirection
- lastSwitchTime = tick()
- end
- else
- humanoid:Move(Vector3.new(0, 0, 0), true) -- Stop movement when not moving
- end
- end
- -- Main function to manage the movement cycle
- local function startMovementCycle()
- while true do
- wait(waitDuration) -- Wait for 60 seconds before starting the movement
- -- Start moving back and forth
- isMoving = true
- local moveStartTime = tick()
- -- Connect the movement function to RenderStepped to start moving
- local connection = runService.RenderStepped:Connect(moveBackAndForth)
- -- Wait for the duration of the movement
- wait(movementDuration)
- -- Stop moving back and forth
- isMoving = false
- connection:Disconnect() -- Disconnect the RenderStepped connection
- -- Stop character movement
- humanoid:Move(Vector3.new(0, 0, 0), true)
- -- Wait another 60 seconds before repeating the cycle
- end
- end
- -- Start the movement cycle
- startMovementCycle()
Advertisement
Add Comment
Please, Sign In to add comment