Movement system, sprinting function using code from the slide function?

Hello,
Recently I’ve started working on a movement system that allows sprinting and sliding.

While working on adding animations to the sliding mechanic, I managed to get it somewhat working, the problem was when I noticed that whenever I sprinted, it acted as if I was sliding.

I haven’t tried much, as i don’t know where to start.

Thanks in advance.

Code:

local userInput = game:GetService("UserInputService")

local players = game:GetService("Players")



local sprintSpeed = 30 

local walkSpeed = 16 
local isrunning = false


local player = players.LocalPlayer



local function beginSprint(input, gameProcessed)

	if not gameProcessed then        

		if input.UserInputType == Enum.UserInputType.Keyboard then

			local keycode = input.KeyCode

			if keycode == Enum.KeyCode.LeftControl then 

				player.Character.Humanoid.WalkSpeed = sprintSpeed
				if isrunning == false then isrunning = true
					
				end
			end

		end

	end

end



local function endSprint(input, gameProcessed)

	if not gameProcessed then

		if input.UserInputType == Enum.UserInputType.Keyboard then

			local keycode = input.KeyCode

			if keycode == Enum.KeyCode.LeftControl then

				player.Character.Humanoid.WalkSpeed = walkSpeed
				if isrunning == true then isrunning = false
					
				end
			end

		end

	end

end


local function duringslide(input, gameProcessed)
if isrunning == true then	
	if not gameProcessed then

		if input.UserInputType == Enum.UserInputType.Keyboard then

			local keycode = input.KeyCode

			if keycode == Enum.KeyCode.C then
					
					-- Import
					local animation = Instance.new("Animation")
					animation.AnimationId = "http://www.roblox.com/Asset?ID=11309578902"
					
					-- local variables
					local animTrack = nil
					local canPlay = true
						
						if canPlay then
							local pc = game.Players.LocalPlayer.Character
							canPlay = false
							animTrack = pc.Humanoid:LoadAnimation(animation)
							
						animTrack:Play()
							
							
						end
					end
				
				player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed * 1.5
				wait(0.1) player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed - 2
				wait(0.1) player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed - 3
				wait(0.1) player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed - 4
				wait(0.1) player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed - 6
					wait(0.1) player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed - 9
					
					end
				end
			end
		end

local function endslide(input, gameProcessed)
	if not gameProcessed then

		if input.UserInputType == Enum.UserInputType.Keyboard then

			local keycode = input.KeyCode

			if keycode == Enum.KeyCode.C then

				player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.walkspeed - 9
				local animation = script:WaitForChild("Animation")
				local animTrack = nil
				local pc = game.Players.LocalPlayer.Character
				
				animTrack = pc.Humanoid:LoadAnimation(animation)
				animTrack.Stop()
				
			
				
				end
			end
		end
	end


userInput.InputBegan:Connect(duringslide)
userInput.InputEnded:Connect(endslide)
	
	userInput.InputBegan:Connect(beginSprint)
userInput.InputEnded:Connect(endSprint)
2 Likes

I feel like it is because you have called the sliding function when ever a player gives an input, note that InputBegan simply can mean any key, instead you can try this:

local userInput = game:GetService("UserInputService")

local players = game:GetService("Players")



local sprintSpeed = 30 

local walkSpeed = 16 
local isrunning = false

local SlideAnimation = Instance.new("Animation")
SlideAnimation.AnimationId = "http://www.roblox.com/Asset?ID=11309578902"


local player = players.LocalPlayer
local Humanoid = player.Character.Humanoid

local slide = Humanoid:LoadAnimation(SlideAnimation)

userInput.InputBegan:Connect(function(input, gameProc)
	if gameProc then
		return
	end
	
	if input.UserInputType == Enum.UserInputType.Keyboard then

		local keycode = input.KeyCode

		if keycode == Enum.KeyCode.LeftControl then 

			player.Character.Humanoid.WalkSpeed = sprintSpeed
			if isrunning == false then
				isrunning = true
			end
		end

	end
	
	if input.KeyCode == Enum.KeyCode.C then
		if isrunning == true then	
			if not gameProc then

				if input.UserInputType == Enum.UserInputType.Keyboard then

					local keycode = input.KeyCode

					if keycode == Enum.KeyCode.C then
						
						local canPlay = true
						if canPlay then
							
							SlideAnimation.Looped = true
							SlideAnimation:Play()


						end
					end

					player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed * 1.5
					wait(0.1) player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed - 2
					wait(0.1) player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed - 3
					wait(0.1) player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed - 4
					wait(0.1) player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed - 6
					wait(0.1) player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed - 9

				end
			end
		end
	end
end)

userInput.InputEnded:Connect(function(input, gameProc)
	if gameProc then
		return
	end
	
	if input.KeyCode == Enum.KeyCode.LeftControl then

		player.Character.Humanoid.WalkSpeed = walkSpeed
		if isrunning == true then
			isrunning = false
		end
		
	elseif input.KeyCode == Enum.KeyCode.C then
		
		player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.walkspeed - 9
		
		SlideAnimation.Stop()
		
	end
	
end)```