Certainly! Here's a Roblox script that accomplishes what you've requested. This script allows you to input your own walking and running animations. When the player starts moving, their walk speed begins at 16 and gradually increases to a maximum of 26. As their speed increases, the walking animation smoothly transitions into the running animation.
Setup Instructions:
-
Place Your Animations:
- In
StarterPlayer > StarterCharacterScripts, create a new LocalScript and name it as you like (e.g., MomentumWalkRunScript).
- Inside this LocalScript, insert two Animation objects:
- Rename one to
WalkingAnimation.
- Rename the other to
RunningAnimation.
- Set the
AnimationId property of each Animation object to the asset ID of your walking and running animations, respectively.
-
Copy the Script:
- Replace the contents of the LocalScript with the following code:
-- LocalScript inside StarterCharacterScripts
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
-- Get the walking and running animations from the script
local walkingAnimation = script:WaitForChild("WalkingAnimation")
local runningAnimation = script:WaitForChild("RunningAnimation")
-- Load the animations
local walkingTrack = humanoid:LoadAnimation(walkingAnimation)
local runningTrack = humanoid:LoadAnimation(runningAnimation)
-- Set initial walk speed and parameters
local minWalkSpeed = 16
local maxWalkSpeed = 26
local timeToMaxSpeed = 5 -- Time (in seconds) to reach maximum speed
-- Variables to control speed increase
local isMoving = false
local speedIncreaseStartTime = 0
-- Play the walking and running animations
walkingTrack:Play()
walkingTrack.Looped = true
runningTrack:Play()
runningTrack.Looped = true
runningTrack:AdjustWeight(0) -- Start with running animation at zero weight
-- Function to start increasing speed
local function startIncreasingSpeed()
if not isMoving then
isMoving = true
speedIncreaseStartTime = tick()
end
end
-- Function to reset speed when stopping
local function resetSpeed()
isMoving = false
humanoid.WalkSpeed = minWalkSpeed
walkingTrack:AdjustWeight(1)
runningTrack:AdjustWeight(0)
end
-- Detect when the player starts or stops moving
humanoid.Running:Connect(function(speed)
if speed > 0 then
startIncreasingSpeed()
else
resetSpeed()
end
end)
-- Update loop to adjust speed and animations
game:GetService("RunService").RenderStepped:Connect(function()
if isMoving then
local elapsedTime = tick() - speedIncreaseStartTime
local progress = math.clamp(elapsedTime / timeToMaxSpeed, 0, 1)
local currentSpeed = minWalkSpeed + (maxWalkSpeed - minWalkSpeed) * progress
humanoid.WalkSpeed = currentSpeed
-- Blend animations based on progress
walkingTrack:AdjustWeight(1 - progress)
runningTrack:AdjustWeight(progress)
else
humanoid.WalkSpeed = minWalkSpeed
end
end)
How the Script Works:
-
Animation Loading:
- The script loads your walking and running animations using
humanoid:LoadAnimation().
- It starts playing both animations but sets the running animation's weight to 0 initially.
-
Speed Control:
- When the player starts moving (
humanoid.Running event with speed > 0), it triggers startIncreasingSpeed().
- The player's
WalkSpeed increases from 16 to 26 over timeToMaxSpeed seconds.
- The increase is smooth, calculated based on the elapsed time since movement started.
-
Animation Blending:
- As the player's speed increases, the script adjusts the weight of each animation to create a smooth transition.
- The
walkingTrack weight decreases from 1 to 0.
- The
runningTrack weight increases from 0 to 1.
-
Stopping Movement:
- When the player stops (
humanoid.Running event with speed == 0), the script resets the speed and animation weights.
Customization:
Notes:
-
Animation Priority:
- Make sure your animations have the appropriate priority set in the animation editor (e.g.,
Movement).
-
Looping Animations:
- The script sets both animations to loop. Ensure your animations are designed to loop seamlessly.
-
Compatibility:
- This script should work with standard Roblox character rigs. If you have a custom character, make sure the animations are compatible.
Troubleshooting:
-
Animations Not Playing:
- Double-check that the
AnimationIds are correct and that the animations are properly uploaded to Roblox.
-
Speed Not Increasing:
- Ensure the script is placed inside
StarterCharacterScripts so that it runs in the context of the player's character.
- Verify that there are no other scripts overriding the
WalkSpeed.
-
Error Messages:
- Check the Output window in Roblox Studio for any error messages that can help identify issues.
Example:
Here's how your script hierarchy should look:
StarterPlayer
└─ StarterCharacterScripts
└─ MomentumWalkRunScript (LocalScript)
├─ WalkingAnimation (Animation)
└─ RunningAnimation (Animation)
By following these instructions, you should have a working system where the player's speed increases from 16 to 26 as they move, with animations smoothly transitioning from walking to running.
If you have any questions or need further assistance, feel free to ask!