My character doesn't sprint when I click the shift key

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)

Its weird how you’re using both CAS and UIS

local function running()
uis.InputBegan:Connect(sprintstartup)
uis.InputEnded:Connect(sprintend)
end

cas:BindAction(“Run”,running,true,Enum.KeyCode.LeftShift)

Which is probably the reason why it doesn’t work

Yeah, I was having some trouble trying to make it work properly, do you want to see the old script I had in mind.

Do you want it IN CAS or UIS though?

cas works better for me personally

alright, so there’s a parameter returned on the function (running) called UserInputState
(its an enum btw)

It can tell you the state of the button being pressed like if its begun being held or ended being held
https://developer.roblox.com/en-us/api-reference/enum/UserInputState

Example:

local function test(actionName,userInputState)
	if userInputState == Enum.UserInputState.Begin then
		print("Input Began")
	end
end

You can use one or two if statements to tell when the input has begun or ended.

ohhhh i see ill fix that right now, ill get back to you in a second.

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

hope this works

It still doesn’t work yet, here is the new code.

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)