MovementHandler (Crouch, Sprint, Slide, Prone)

You really made this guy post an entire License agreement for a simple script :skull_and_crossbones:

47 Likes

This isn’t github, you can give you the code.

5 Likes

The animations might be invisible because you don’t own them. I’m pretty sure it’s a security feature.

@GiantDefender427 try setting these models on sale

https://www.roblox.com/library/7932218248/CrouchIdle
https://www.roblox.com/library/7932221054/CrouchWalk
https://www.roblox.com/library/7921074914/Slide

Also, a critique on the code:

image

Since this runs on the Client side, you can eliminate the Player parameter. Just say Player = game.Players.LocalPlayer since it doesn’t make sense to call this on any other player.

1 Like

Well those animations aren’t really high quality so i never thought people would want to be using them, but yes I can put it on sale no problem.

I have also included the Key frame sequence if you look inside the animation objects.

1 Like

oh sorry, i didn’t see that part of your post

It’s nice to have a LICENSE but realistically no one cares in Roblox development.

I believe if you have your code published to Roblox you’re stuck under specific conditions anyway.

4 Likes

Oh no I have 2 topics now what do I do?

1 Like

Update

I got some feedback from professional developers and I think they have a strong point, so I made a few changes. Now you can put the script in StarterPlayerScripts and that’s all, no worries if the character dies, script automatically adjusts.

1 Like

It doesn’t work even though I put my own animation only thing I changed in the script was
States.Crouch and States.Slide to True :man_shrugging:t5: can you please help me? Is there a way to change Keybinds and how would I configure a prone system in your script?

-- State

local States = {}

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

-- Load Animations

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


image

You need to change the AnimationId property of the Animation, not the variable.

Doing that will definitely cause errors, and does not seem to be necessary. To change the keybind refer to the BindAction function of ContextActionService, as that is what is used for player input.

Prone system is till not yet confirmed due to quality. In Crouch the camera does not move accordingly, in Slide as well. Till I ensure quality, I won’t be looking forward to Prone.

1 Like

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!