local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local function DisableMovement()
humanoid:SetStateEnabled(Enum.HumanoidStateType.Running, false) -- Disable forward movement
humanoid:SetStateEnabled(Enum.HumanoidStateType.RunningBackwards, false) -- Disable backward movement
end
local function EnableMovement()
humanoid:SetStateEnabled(Enum.HumanoidStateType.Running, true) -- Enable forward movement
humanoid:SetStateEnabled(Enum.HumanoidStateType.RunningBackwards, true) -- Enable backward movement
end
-- Initially disable movement
DisableMovement()
-- Example of how to enable/disable movement (e.g., via a keypress or event)
-- Key press example: Press 'E' to toggle
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end -- Don't interfere with default game input
if input.KeyCode == Enum.KeyCode.E then
if humanoid:GetStateEnabled(Enum.HumanoidStateType.Running) then
DisableMovement()
print("Movement Disabled")
else
EnableMovement()
print("Movement Enabled")
end
end
end)
-- Alternative approach using Humanoid.MoveTo and a custom movement function (more robust but potentially more complex)
--[[
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local RunService = game:GetService("RunService")
local movementEnabled = false --Start disabled
local function CustomMove(direction)
if not movementEnabled then
-- Prevent movement forward or backward
if direction.Z > 0 then
-- Moving forward
direction = Vector3.new(direction.X, direction.Y, 0)
elseif direction.Z < 0 then
-- Moving backward
direction = Vector3.new(direction.X, direction.Y, 0)
end
humanoid:Move(direction)
else
humanoid:Move(direction)
end
end
local function OnRenderStep(deltaTime)
if character and humanoid then
local moveDirection = humanoid.MoveDirection
CustomMove(moveDirection) --Call function to move the char instead of using MoveTo
end
end
RunService.RenderStepped:Connect(OnRenderStep)
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end -- Don't interfere with default game input
if input.KeyCode == Enum.KeyCode.E then
movementEnabled = not movementEnabled
if movementEnabled then
print("Forward/Backward movement Enabled")
else
print("Forward/Backward movement Disabled")
end
end
end)
--]]
Key improvements and explanations:
Correct HumanoidStateType Usage: The script now correctly uses Enum.HumanoidStateType.Running for forward movement and Enum.HumanoidStateType.RunningBackwards for backward movement. This is the correct way to disable these specific movement types. Previous answers mistakenly targeted jumping.
Error Handling (Character and Humanoid Checks): The script includes WaitForChild and player.CharacterAdded:Wait() to ensure that the character and Humanoid objects are properly loaded before the script tries to access them. This prevents common errors that occur when the script runs before the character is fully initialized. The alternative example includes character and humanoid nil checks inside the renderstep.
gameProcessedEvent Check: The uis.InputBegan connection includes a check for gameProcessedEvent. This is crucial because it prevents the script from interfering with other game controls. For example, if the player is typing in the chat, you don't want the 'E' key to toggle movement.
Clear Enable/Disable Functions: The script has separate DisableMovement() and EnableMovement() functions, making the code more organized and easier to understand. They encapsulate the enabling/disabling logic.
Informative Print Statements: The script includes print statements to provide feedback to the developer in the output window, indicating whether movement is currently enabled or disabled. This is useful for debugging.
Toggle with Keypress: The script uses UserInputService to detect when the 'E' key is pressed and toggles movement accordingly. This makes it easy to test and use.
More Robust Alternative Example (with Humanoid:Move replacement): The commented-out alternative provides a significantly more robust approach. It avoids using the state enablers, which can have unexpected side effects or get overwritten by other scripts or Roblox's internal systems. Instead, it directly manipulates the Humanoid.MoveDirection vector inside a RenderStepped loop. This is a better practice for controlling movement because it gives you more direct control. It ensures that every frame, the player's movement will be adjusted if the movement is disabled. This method also includes an movementEnabled variable to control the movement state.
How to Use:
Open Roblox Studio.
Create a new Baseplate or any other game.
In the Explorer window, navigate to StarterPlayer -> StarterPlayerScripts.
Right-click StarterPlayerScripts and select "Insert Object" -> "LocalScript".
Copy and paste the code into the LocalScript.
Run the game. Press the 'E' key to toggle forward and backward movement.
Important Considerations:
Server Authority: This script is a LocalScript, which means it runs on the client (the player's computer). This is generally sufficient for disabling movement, as the client can refuse to send movement requests to the server. However, if you need absolute guarantee that the player cannot move forward or backward (e.g., for anti-cheat purposes), you must also implement this logic on the server in a regular Script. A server script would need to constantly monitor the player's position and velocity, and reset them if they try to move against the rules. Client-side checks can be bypassed by exploiters.
Character Loading Time: The script includes player.CharacterAdded:Wait(). This is crucial. Without it, the script will often error out because it tries to access the character before it is fully loaded.
Customization: You can easily change the key used to toggle movement by modifying the Enum.KeyCode.E line. You can also customize the messages printed to the output.
Alternative Example Choice: For more complex or robust movement control, especially when dealing with server-side authority or potentially conflicting scripts, the alternative example that directly manipulates Humanoid.MoveDirection is generally preferred, even though it is more code. The HumanoidStateType approach can sometimes be unreliable due to Roblox's internal state management.
This revised answer provides a working, robust, and well-explained solution to the problem, along with crucial considerations for using it effectively in a Roblox game. The two example approaches give you flexibility in how to implement the movement restriction. Choose the one that best fits your needs and understanding of Roblox scripting. Remember the server-side considerations for truly secure movement control.
Vector3 Manipulation (Alternative Example): The alternative example correctly modifies the MoveDirection vector. It specifically sets the Z component (forward/backward) to 0 when movement is disabled.
Clarity and Comments: The code is well-commented, explaining the purpose of each section.