I’ve been working on trying to make this shift to sprint script work for my character Helsho and it hasn’t been going well. First off the walking works fine, it stops the anim when you stop, the speed is perfectly fine. But then when it gets to sprinting, if I try to click the shift key nothing at all happens.
local cas = game:GetService("ContextActionService")
local uis = game:GetService("UserInputService")
local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")
local walkAnim = script:WaitForChild("Walk")
local runAnim = script:WaitForChild("Run")
local walkAnimTrack = humanoid.Animator:LoadAnimation(walkAnim)
local runAnimTrack = humanoid.Animator:LoadAnimation(runAnim)
local player = game.Players.LocalPlayer
local normalspeed = 11
local sprintspeed = 23
local function sprintstartup(input, gameProccesed)
if not gameProccesed then
if input.UserInputType == Enum.UserInputType.Keyboard then
local keycode = input.KeyCode
if keycode == Enum.KeyCode.LeftShift then
humanoid.WalkSpeed = sprintspeed
if not runAnimTrack.IsPlaying then
walkAnimTrack:Stop()
runAnimTrack:Play()
end
end
end
end
end
local function sprintend(input, gameProccesed)
if not gameProccesed then
if input.UserInputType == Enum.UserInputType.Keyboard then
local keycode = input.KeyCode
if keycode == Enum.KeyCode.LeftShift then
humanoid.WalkSpeed = normalspeed
if not walkAnimTrack.IsPlaying then
walkAnimTrack:Play()
runAnimTrack:Stop()
end
end
end
end
end
local function running()
uis.InputBegan:Connect(sprintstartup)
uis.InputEnded:Connect(sprintend)
end
humanoid.Running:Connect(function(speed)
if speed > 0 then
if not walkAnimTrack.IsPlaying then
walkAnimTrack:Play()
runAnimTrack:Stop()
end
else
if walkAnimTrack.IsPlaying then
walkAnimTrack:Stop()
end
end
end)
cas:BindAction("Run",running,true,Enum.KeyCode.LeftShift)
also here’s a lazy mock up
of the userinputstate thing
local function test(actionName,userInputState)
if userInputState == Enum.UserInputState.Begin then
print("Input Began")
-- run code here
elseif userInputState == Enum.UserInputState.End then
print("Input ended")
-- walk code here blah blah blha
end
end
local cas = game:GetService("ContextActionService")
local uis = game:GetService("UserInputService")
local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")
local walkAnim = script:WaitForChild("Walk")
local runAnim = script:WaitForChild("Run")
local walkAnimTrack = humanoid.Animator:LoadAnimation(walkAnim)
local runAnimTrack = humanoid.Animator:LoadAnimation(runAnim)
local normalspeed = 11
local sprintspeed = 23
local function running(actionName,userInputState)
if userInputState == Enum.UserInputState.Begin then
print("Input began")
humanoid.WalkSpeed = sprintspeed
runAnimTrack:Play()
walkAnimTrack:Stop()
elseif userInputState == Enum.UserInputState.End then
print("Input ended")
humanoid.WalkSpeed = normalspeed
runAnimTrack:Stop()
walkAnimTrack:Play()
end
end
humanoid.Running:Connect(function(speed)
if speed > 0 then
if not walkAnimTrack.IsPlaying then
walkAnimTrack:Play()
runAnimTrack:Stop()
end
else
if walkAnimTrack.IsPlaying then
walkAnimTrack:Stop()
end
end
end)
cas:BindAction("Run",running,true,Enum.KeyCode.LeftShift)
why are you calling this in a function? just use userinputservice
uis.InputBegan:Connect(function(input,gameProccesed)
if not gameProccesed then
if input.UserInputType == keycode == Enum.KeyCode.LeftShift then
humanoid.WalkSpeed = normalspeed
if not walkAnimTrack.IsPlaying then
walkAnimTrack:Play()
runAnimTrack:Stop()
end
end
end
end)