MovementHandler (Crouch, Sprint, Slide, Prone)

I added acceleration to your script @GiantDefender427

Edited script
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local Camera = game.Workspace.CurrentCamera
local TS = game:GetService("TweenService") 
local plr = game.Players.LocalPlayer

local Character = nil
local Humanoid = nil
local HumanoidRunningConnection = nil

--\\ State

local States = {}

States.Sprint = false
States.Crouch = false
States.Slide = false

--\\ Sprint and Walk Speed

local SprintSpeed = 23
local WalkSpeed = 16

--\\ Load Animations

local CrouchIdleAnim = "rbxassetid://8087634282"
local CrouchWalkAnim = "rbxassetid://8087642001"
local SlideAnim = "nil"

--\\ Field Of View settings

local function lerp(a, b, t)
	return a * (1 - t) + (b * t)
end

local function SpringFov(Fov)
	task.spawn(function()
		local num = 0
		while math.abs(num - Fov) > 0.01 do
			num = lerp(num, Fov, 0.77)
			local rec = num
			Camera.FieldOfView += rec
			RunService.RenderStepped:Wait()
		end
	end)
end 

--\\ Sprinting

local function Sprint(ActionName, InputState, InputObject)
	if not Character then return end
	if not ActionName == "Sprint" then return end

	if InputState == Enum.UserInputState.Begin then
		States.Crouch = false
		States.Sprint = true

		CrouchIdleAnim:Stop()
		CrouchWalkAnim:Stop()
		
--\\ Tweening
		
		**Humanoid.WalkSpeed = SprintSpeed
		UserInputService.InputBegan:Connect(function(key) -- 
			if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.RightShift then
				TS:Create(plr.Character.Humanoid, TweenInfo.new(10), {WalkSpeed = 23}):Play()
			end
		end)

		UserInputService.InputEnded:Connect(function(key) -- 
			if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.RightShift then
				TS:Create(plr.Character.Humanoid, TweenInfo.new(10), {WalkSpeed = 16}):Play()
			end
		end)

		SpringFov(SprintSpeed/6)
	else
		States.Sprint = false

		Humanoid.WalkSpeed = WalkSpeed

		SpringFov(-SprintSpeed/6)
	end
end

--\\ Crouching

local function Crouch(ActionName, InputState, InputObject)
	if not Character then return end

	if States.Crouch == false then
		States.Crouch = true

		CrouchIdleAnim:Play()

		Humanoid.WalkSpeed = 10
	elseif States.Crouch == true then
		States.Crouch = false

		CrouchIdleAnim:Stop()
		CrouchWalkAnim:Stop()

		Humanoid.WalkSpeed = WalkSpeed
	end
end

--\\ Sliding

local function Slide(ActionName, InputState, InputObject)
	if not Character then return end

	States.Crouch = false
	States.Slide = true

	CrouchIdleAnim:Stop()
	CrouchWalkAnim:Stop()

	local SlideVelocity = Instance.new("BodyVelocity")
	SlideVelocity.MaxForce = Vector3.new(1,1,1) * 50000
	SlideVelocity.Velocity = Character.HumanoidRootPart.CFrame.LookVector * 50

	-- R15
	if Character:FindFirstChild("LowerTorso") then
		SlideVelocity.Parent = Character.LowerTorso
	end

	--R6
	if Character:FindFirstChild("Torso") then
		SlideVelocity.Parent = Character.Torso
	end

	-- Start Sliding

	SlideAnim:Play()

	task.wait(1)

	-- End Sliding

	States.Slide = false
	SlideVelocity:Destroy()
	SlideAnim:Stop()
end

--\\ Crouch and Slide handler

local function CrouchAndSlide(ActionName, InputState, InputObject)
	if InputState == Enum.UserInputState.Begin then else return end

	if States.Sprint == false and States.Slide == false then
		Crouch()
	elseif States.Sprint == true and States.Slide == false then
		Slide()
	end
end

--\\ ContextActionService Input

ContextActionService:BindAction("Sprint", Sprint, false, Enum.KeyCode.LeftShift)
ContextActionService:BindAction("CrouchAndSlide", CrouchAndSlide, false, Enum.KeyCode.C, Enum.KeyCode.LeftControl)

local function HumanoidRunning(Speed)
	if States.Crouch == true then
		if Speed > 0 then
			CrouchIdleAnim:Stop()
			if not CrouchWalkAnim.IsPlaying then
				CrouchWalkAnim:Play()
			end
		else
			CrouchWalkAnim:Stop()
			if not CrouchIdleAnim.IsPlaying then
				CrouchIdleAnim:Play()
			end
		end
	elseif States.Crouch == false then
		CrouchIdleAnim:Stop()
		CrouchWalkAnim:Stop()
	end
end

game.Players.LocalPlayer.CharacterAdded:Connect(function(char)
	Character = char
	Humanoid = Character:WaitForChild("Humanoid")
	CrouchIdleAnim = Humanoid.Animator:LoadAnimation(script:WaitForChild("CrouchIdle"))
	CrouchWalkAnim = Humanoid.Animator:LoadAnimation(script:WaitForChild("CrouchWalk"))
	SlideAnim = Humanoid.Animator:LoadAnimation(script:WaitForChild("Slide"))
	HumanoidRunningConnection = Humanoid.Running:Connect(HumanoidRunning)
end)

and hereā€™s a video:

robloxapp-20211126-1702562.wmv (2.2 MB) Ps: sorry for the ā€œrainā€ itā€™s an in-game weather system I was testing

1 Like

I know that this is completely inaccurate to the topic, but your weather system causes strain on the server.
You should add some more particle emitters and cull them according to the players camera. ALL OF THIS SHOULD HAPPEN ON THE CLIENT. In order to clone stuff and put them to the workspace FROM THE CLIENT, simply put the stuff to clone in ReplicatedStorage. I do not intend to ctiticize, I intend to help. Sorry for the irrelevant to the topic disturbance.

1 Like

PPlayer31, next time please take this to the messages.

OH YES I FORGOT! Sorry for that!

1 Like

Quick question, for the animations would we move the player down or does the script handle that?
And what about the Animation priorities

I did not understand what you mean by thisā€¦

He basically means - when making a animation, or the animations for crouching, does the script move the playerā€™s humanoidrootpart/character down automatically, or do we have to manually animate the character down.

1 Like

No the animation does not move the HumanoidRootPart, and I donā€™t know the need to move it in the first placeā€¦ if it is to move the Camera, Humanoid.CameraOffset is what will be used. And if it is about moving the body parts down, yes the Animation does that.

Update

Alright everyone update night(Iā€™m in India so bear with me), todayā€™s changes are:

  • Animation speed depending on WalkSpeed
  • Camera moves up and down while Sliding and Crouching :exploding_head:

This time the code is also a lot better formatted and placed so hopefully things will be a bit easier to understand, thatā€™s all!!

Make sure to drop your feedback and a like down below :wink:

For the oneā€™s who are wondering, I will be adding Prone in the next update, I am trying not to rush things so stay tuned!

Update

Prone is now added as well, surprisingly easy to make, kind of scary but anyways, enjoy!!

1 Like

Update

I got more feedback from cool developers(yes I love to do that), and now the Module is Object-Oriented!! and simpler to use!!

  • Enjoy!!
  • Drop a like
  • and some feedback
2 Likes

Hello, you have made an awsome contribution!

I would like to make a few of sugguestion for making things for the community or for anything open source in general.

  1. The instructions on how to use this module is somewhat unclear. On initial use attempt, your users would not be aware that they must create a folder with animations and name the animations with specific names accordingly.
    Expected: Animations provided with proper names and folder parented under the module or inscrutions telling users to give certain animations parented under a folder name ā€œAnimationFolderā€ under ReplicatedStorage.

  2. No basic API documentation was provided. Besides insurctions on how to initiate the module no instrcutions on how to use or what the module could do, although the method names are self-explainitory.
    Expected: Some form of API documentation stating how or what the module can do. You can paste a bare bones list of methods with the comment section at the header for your module.

Example:

--[[
    -- SETUP:
    1. Place module within game.ReplicatedStorage.
    2. Create a folder named "Animations" parented by game.ReplicatedStorage.
    3. Place animations to game.ReplicatedStorage.Animations with the names: CrouchIdle, CrouchWalk, ProneIdle, ProneWalk, Slide


    -- API:
    local MovementProfile = require(game.ReplicatedStorage.MovementHandler.MovementHandler)

    local movementHandle = MovementProfile:New({Player = game.Players.GiantDefender427})

    movementHandle:Initiate()
    movementHandle:StartSprinting()
    movementHandle:StopSprinting()
    movementHandle:StartCrouching()
    movementHandle:StopCrouching()
    movementHandle:StartProning()
    movementHandle:StopProning()
]]
2 Likes

Hi,

Looks very good but having issues trying to set up, do you have an example uncopylocked site to show how to configure?

Thanks

1 Like

the instructions are unclear, and i am very confused as to what i am supposed to do with this movement handler. where are we even supposed to put everything??

1 Like

Could you provide a couple of videos, or a test place?

2 Likes

Only post resources with a substantial free or open-source component here

For anyone who wants to know, everything posted here (#resources:community-resources) is open-source, and has to be.

1 Like

Small update

Since a lot of you asked for this, I made uploaded a test place, you can download the test place here. Make sure to check out the files in ReplicatedStorage and StarterPlayerScripts.

2 Likes

this is helpful, now i (and a lot of others) can see how it works and how itā€™s set up. thank you!

1 Like

sorry for so many replies and questions, but is there a way to disable some states? for example if i wanted to disable sprinting, could i do that?

Youā€™d would have to modify the code a bit, but Iā€™ll add the feature you requested as soon as my exam ends (March 16), thatā€™s a good idea tbh.

1 Like