#How to make my sprint mechanic work?

1 messages · Page 1 of 1 (latest)

desert garnet
#

local plr = game.Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")

local walkSpeed = 10
local sprintSpeed = 32
local speedBuildTime = 0.5 -- seconds to reach full sprint speed
local humanoid = nil
local sprinting = false

-- Find AnimationController and Animation objects under this script
local animController = script:FindFirstChild("SprintAnimController")
local speedBurstAnimObj = nil
local sprintAnimObj = nil

if animController then
speedBurstAnimObj = animController:FindFirstChild("SpeedBurst")
sprintAnimObj = animController:FindFirstChild("Sprint")
end

local speedBurstTrack = nil
local sprintTrack = nil

local function stopAndDestroy(track)
if track then
track:Stop()
track:Destroy()
end
end

local function updateHumanoid()
if plr.Character then
humanoid = plr.Character:FindFirstChild("Humanoid")
if humanoid then
humanoid.WalkSpeed = walkSpeed
-- Clean up previous tracks
stopAndDestroy(speedBurstTrack)
stopAndDestroy(sprintTrack)
speedBurstTrack = nil
sprintTrack = nil
-- Load animations if available
if speedBurstAnimObj then
speedBurstTrack = humanoid:LoadAnimation(speedBurstAnimObj)
end
if sprintAnimObj then
sprintTrack = humanoid:LoadAnimation(sprintAnimObj)
end
end
end
end

desert garnet
# desert garnet local plr = game.Players.LocalPlayer local UserInputService = game:GetService("U...

plr.CharacterAdded:Connect(function()
updateHumanoid()
end)

updateHumanoid()

local function buildUpSprint()
if not humanoid then return end
local startSpeed = humanoid.WalkSpeed
local targetSpeed = sprintSpeed
local elapsed = 0
sprinting = true
-- Play speed burst animation
if speedBurstTrack then
speedBurstTrack:Play()
speedBurstTrack.Looped = false
end
while sprinting and humanoid and humanoid.WalkSpeed < targetSpeed do
elapsed = elapsed + task.wait()
local alpha = math.clamp(elapsed / speedBuildTime, 0, 1)
humanoid.WalkSpeed = startSpeed + (targetSpeed - startSpeed) * alpha
if alpha >= 1 then
humanoid.WalkSpeed = targetSpeed
break
end
end
-- Stop speed burst animation and play sprint animation
if speedBurstTrack then
speedBurstTrack:Stop()
end
if sprintTrack then
sprintTrack:Play()
sprintTrack.Looped = true
end
end

local function buildDownSprint()
if not humanoid then return end
local startSpeed = humanoid.WalkSpeed
local targetSpeed = walkSpeed
local elapsed = 0
local downTime = speedBuildTime
-- Stop both animations
if sprintTrack then
sprintTrack:Stop()
end
if speedBurstTrack then
speedBurstTrack:Stop()
end
while not sprinting and humanoid and humanoid.WalkSpeed > targetSpeed do
elapsed = elapsed + task.wait()
local alpha = math.clamp(elapsed / downTime, 0, 1)
humanoid.WalkSpeed = startSpeed + (targetSpeed - startSpeed) * alpha
if alpha >= 1 then
humanoid.WalkSpeed = targetSpeed
break
end
end
end

#

UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.LeftShift then
if humanoid and not sprinting then
task.spawn(buildUpSprint)
end
end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.LeftShift then
if sprinting then
sprinting = false
task.spawn(buildDownSprint)
end
end
end)

#

idk if this is a good explanation. I have this script in a seperate folder in the StarterPlayerScripts

vivid trail
full urchinBOT
#

studio** You are now Level 1! **studio

desert garnet
desert garnet
#

R6

vivid trail
#

you set the animation prioritys right?

desert garnet
#

Also i have another weird bug now. So I added console controls for sprinting and now when ever you just hold w it does the sprint and to slow down you just tap shift. Which is weird.

full urchinBOT
#

studio** You are now Level 2! **studio

desert garnet
vivid trail
#

are you in studio rn

desert garnet
vivid trail
#

is that the full code?

desert garnet
#

Yeah i sent everything

vivid trail
#

you have the animaiton id of all animaitons right?

desert garnet
#

Yep

vivid trail
desert garnet
#

I had the animations parented to the script and just referenced them

#

Does that not work

desert garnet
vivid trail
#

this is all parented to humanoid right?

desert garnet
#

Uh. I think so

#

Lemme js turn back on my computer rq. I'll js fix it rn.

vivid trail
#

wont starterplayerscripts just put the scripts under the player, not the humanoid?

desert garnet
#

OHHHHH

#

YOUR RIGHT

vivid trail
#

maybe do smth like this

#

local sprintAnim = Instance.new("Animation")
sprintAnim.AnimationId = "rbxassetid://IDK CODE THINGY"
local sprintTrack = humanoid:LoadAnimation(sprintAnim)

#

then just use sprintTrack:Play()

#

and sprintTrack:Stop()

#

or smth

vivid trail
full urchinBOT
#

studio** You are now Level 2! **studio

vivid trail
desert garnet
#

The sprint one yes. The speed build up, no.

vivid trail
#

think that should work

desert garnet
#

Alright let me see

#

This is gonna take a minute. My pc has to update for some reason

desert garnet
#

Yes 😭

vivid trail
#

i sure love the activate windows watermark

desert garnet
#

Im doing all of this and idek if ima keep sprinting 💔

desert garnet
#

Yeah idk

full urchinBOT
#

studio** You are now Level 3! **studio

vivid trail
desert garnet
#

Yep

#

And it didnt work

#

Im lowkey js rewriting it all bc I think I overcomplicated it.

vivid trail
#

local camera = game.Workspace.CurrentCamera
local TweenService = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer

local SprintAnim = Instance.new("Animation")
SprintAnim.AnimationId = "rbxassetid://85916295470262"

local WalkAnim = Instance.new("Animation")
WalkAnim.AnimationId = "rbxassetid://100498936469692"

local normalFOV = 70
local sprintFOV = 80
local tweenTime = 0.3
local isSprinting = false
local SprintTrack
local WalkTrack
local Humanoid

local function tweenFOV(targetFOV)
local tweenInfo = TweenInfo.new(tweenTime, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local fovTween = TweenService:Create(camera, tweenInfo, {FieldOfView = targetFOV})
fovTween:Play()
end

UIS.InputBegan:Connect(function(input, processed)
if input.UserInputType == Enum.UserInputType.Keyboard and not processed then
if input.KeyCode == Enum.KeyCode.LeftShift and not isSprinting and Humanoid then
isSprinting = true
Humanoid.WalkSpeed = 24
tweenFOV(sprintFOV)

        if WalkTrack and WalkTrack.IsPlaying then WalkTrack:Stop() end
        if SprintTrack then SprintTrack:Play() end
    end
end

end)

#

UIS.InputEnded:Connect(function(input, processed)
if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift and isSprinting and Humanoid then
isSprinting = false
Humanoid.WalkSpeed = 16
tweenFOV(normalFOV)

    if SprintTrack then SprintTrack:Stop() end
end

end)

local function onCharacterAdded(character)
Humanoid = character:WaitForChild("Humanoid")

SprintTrack = Humanoid:LoadAnimation(SprintAnim)
WalkTrack = Humanoid:LoadAnimation(WalkAnim)

Humanoid.WalkSpeed = 16

Humanoid.Running:Connect(function(speed)
    if speed > 0.1 then
        if isSprinting then
            if SprintTrack and not SprintTrack.IsPlaying then SprintTrack:Play() end
            if WalkTrack and WalkTrack.IsPlaying then WalkTrack:Stop() end
        else
            if WalkTrack and not WalkTrack.IsPlaying then WalkTrack:Play() end
            if SprintTrack and SprintTrack.IsPlaying then SprintTrack:Stop() end
        end
    else
        if WalkTrack and WalkTrack.IsPlaying then WalkTrack:Stop() end
        if SprintTrack and SprintTrack.IsPlaying then SprintTrack:Stop() end
    end
end)

end

if Player.Character then
onCharacterAdded(Player.Character)
end
Player.CharacterAdded:Connect(onCharacterAdded)

#

i got this script

#

theres no speedburst thing tho

vivid trail
desert garnet
vivid trail
vivid trail
#

you need to put in ur own animations tho cuz roblox doesnt like sharing aniamtions

desert garnet
#

ok

#

thank you

vivid trail
desert garnet
# vivid trail k

I put my own animation id but it didnt work. Is there something else I need to do?

desert garnet
#

Oh yeah i forgot to change that

#

What does it need to be

vivid trail
#

hmm

#

what is it set to rn

desert garnet
#

Movement

vivid trail
#

core works for me for some reason

vivid trail
desert garnet
#

Nah it aint working

vivid trail
#

hmm

#

im playing it rn it works perfectly

#

is it just going into shiftlock?

vivid trail
desert garnet
#

No

#

Maybe my shitty pc just aint loading it. Thats probably the problem 💔

vivid trail
desert garnet
vivid trail
#

also did u change anything other than the id's

desert garnet
#

Nope

vivid trail
#

ok no idk whats happeneing

#

is it faster tho

desert garnet
#

I only changed the sprint id. Is that the problem?

vivid trail
desert garnet
#

Didn't work

#

Do I need an to add an animation in the localscript?

vivid trail
#

is there any errors in the script

#

like red lines

vivid trail
#

u just need the id's

desert garnet
#

No. There is no errors

vivid trail
#

its working perfectly for me

desert garnet
vivid trail
#

uh

#

hmm

#

the only error thats happening is that the animations arent playing right?

vivid trail
desert garnet
vivid trail
#

like have a script that just loads the animation then starts playing it

vivid trail
#

that will just load and play it

desert garnet
#

Ok hold on

desert garnet
full urchinBOT
#

studio** You are now Level 4! **studio

desert garnet
#

local sprintAnim = Instance.new("Animation")
sprintAnim.AnimationId = "123382623519254"
local sprintTrack = script.Parent:LoadAnimation(sprintAnim)

sprintTrack:Play()
print("success")

vivid trail
#

it needs to start with

#

"rbxassetid://"

#

so dont put "123382623519254"

desert garnet
#

Oh yeah

vivid trail
#

"rbxassetid://123382623519254"

#

like that

#

k does that work now?

desert garnet
#

Uh

#

Still no

vivid trail
#

ok uh

full urchinBOT
#

studio** You are now Level 4! **studio

vivid trail
#

what is script.parent

#

what is it parented too

desert garnet
vivid trail
#

try using an animation object

desert garnet
vivid trail
#

this thing

desert garnet
#

Ok.

#

Uh

#

I'll try that tomorrow. I must sleep. It late.