Crouch not working as I want

Hey,

I am making 1st person game and I want sprint, crouch and smooth movements. Movement looks perfect. I haven’t started making the sprint system but I tried to make shift system but it is not working as I want.

Problems:

  1. Crouch animation does not start playing when holding shift-button
  2. Camera is not going down when crouch and up when not holding shift-button
  3. Crouch animation not showing to other players (I may already have solution for this but you can help me with this also if you want to.)

Codes are here:

Smooth Walk (local script):

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Camera = game:GetService("Workspace").CurrentCamera
local UserInputService = game:GetService("UserInputService")
local Humanoid = Player.Character:WaitForChild("Humanoid")
local shift = false
local bobbing = nil
local func1 = 0
local func2 = 0
local func3 = 0
local func4 = 0
local val = 0
local val2 = 0
local int = 5
local int2 = 5
local vect3 = Vector3.new()

local m = Player:GetMouse()
m.Icon = "http://www.roblox.com/asset/?id=569021388"

--UserInputService.MouseIconEnabled = false

function lerp(a, b, c)
	return a + (b - a) * c
end

bobbing = game:GetService("RunService").RenderStepped:Connect(function(deltaTime)
	deltaTime = deltaTime * 30
	if Humanoid.Health <= 0 then
		bobbing:Disconnect()
		return
	end
	local rootMagnitude = Humanoid.RootPart and Vector3.new(Humanoid.RootPart.Velocity.X, 0, Humanoid.RootPart.Velocity.Z).Magnitude or 0
	local calcRootMagnitude = math.min(rootMagnitude, 25)
	if deltaTime > 1.5 then
		func1 = 0
		func2 = 0
	else
		func1 = lerp(func1, math.cos(tick() * 0.5 * math.random(7.5, 10)) * (math.random(6, 10) / 100) * deltaTime, 0.05 * deltaTime)
		func2 = lerp(func2, math.cos(tick() * 0.5 * math.random(5, 9)) * (math.random(1, 5) / 100) * deltaTime, 0.05 * deltaTime)
	end
	Camera.CFrame = Camera.CFrame * (CFrame.fromEulerAnglesXYZ(0, 0, math.rad(func3)) * CFrame.fromEulerAnglesXYZ(math.rad(func4 * deltaTime), math.rad(val * deltaTime), val2) * CFrame.Angles(0, 0, math.rad(func4 * deltaTime * (calcRootMagnitude / 5))) * CFrame.fromEulerAnglesXYZ(math.rad(func1), math.rad(func2), math.rad(func2 * 10)))
	val2 = math.clamp(lerp(val2, -Camera.CFrame:VectorToObjectSpace((Humanoid.RootPart and Humanoid.RootPart.Velocity or Vector3.new()) / math.max(Humanoid.WalkSpeed, 0.01)).X * 0.04, 0.1 * deltaTime), -0.12, 0.1)
	func3 = lerp(func3, math.clamp(UserInputService:GetMouseDelta().X, -2.5, 2.5), 0.25 * deltaTime)
	func4 = lerp(func4, math.sin(tick() * int) / 5 * math.min(1, int2 / 10), 0.25 * deltaTime)
	if rootMagnitude > 1 then
		val = lerp(val, math.cos(tick() * 0.5 * math.floor(int)) * (int / 200), 0.25 * deltaTime)
	else
		val = lerp(val, 0, 0.05 * deltaTime)
	end
	if rootMagnitude > 6 then
		int = 10
		int2 = 9
	elseif rootMagnitude > 0.1 then
		int = 6
		int2 = 7
	else
		int2 = 0
	end
	Player.CameraMaxZoomDistance = 128
	Player.CameraMinZoomDistance = 0.5
	vect3 = lerp(vect3, Camera.CFrame.LookVector, 0.125 * deltaTime)
end)

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent then
		if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
			Camera.CFrame = Camera.CFrame * CFrame.new(0, -2, 0)
		end
	end
end)


UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent then
		if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
			Camera.CFrame = Camera.CFrame * CFrame.new(0, 2, 0)
		end
	end
end)

Crouch (local script):




local Player = game.Players.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()

local Humanoid = Character:WaitForChild("Humanoid")

local crouchAnimation = game.ReplicatedStorage.crouch
local walkAnimation = game.ReplicatedStorage.CrouchLoop
local crouchIdleAnimation = game.ReplicatedStorage.CrouchIdle



local UserInputService = game:GetService("UserInputService")

local TweenService = game:GetService("TweenService")



local AnimationID = "15411945306"

local CameraOffsetOn = Vector3.new(0, 0, 0)

local CameraOffsetOff = Vector3.new(0, 0, 0)



local WalkSpeedCrouched = 8 --CROUCH SPEED

local WalkSpeedStanding = 16

local PlaybackSpeedCrouched = 1

local PlaybackSpeedStanding = 1




local CrouchAnimation = Humanoid:LoadAnimation(walkAnimation)
local CrouchIdleAnimation = Humanoid:LoadAnimation(crouchIdleAnimation)

local Crouching = false

local function setCrouchState(isCrouching)
	Crouching = isCrouching
	if isCrouching then

		--Humanoid.WalkSpeed = WalkSpeedCrouched

		TweenService:Create(Humanoid, TweenInfo.new(0.25), { CameraOffset = CameraOffsetOn }):Play()

		--Character.HumanoidRootPart.Running.PlaybackSpeed = PlaybackSpeedCrouched

		--Character.HumanoidRootPart.CanCollide = false

		



	else

		Humanoid.WalkSpeed = WalkSpeedStanding

		TweenService:Create(Humanoid, TweenInfo.new(0.25), { CameraOffset = CameraOffsetOff }):Play()

		--Character.HumanoidRootPart.Running.PlaybackSpeed = PlaybackSpeedStanding

		--Character.HumanoidRootPart.CanCollide = true

		CrouchIdleAnimation:Stop()
		CrouchAnimation:Stop()
	end

end



UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)

	if not gameProcessedEvent then

		if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
			setCrouchState(true)

		end

	end

end)



UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)

	if not gameProcessedEvent then

		if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then

			setCrouchState(false)

		end

	end

end)

Humanoid.Running:Connect(function(speed)
	if Crouching then
		if speed > 0 then
			CrouchAnimation:Play()
			CrouchIdleAnimation:Stop()
		else
			CrouchIdleAnimation:Play()
			CrouchAnimation:Stop()
		end

	end
end)
2 Likes

Please. I need help as fast as possible

3 Likes

um, i think i found the problem of the camera, why your not setting the position of the cam y to -1 or something? that’s why the camera’s not working.

2 Likes

these are the same, so the camera wont go up and down.

sorry, cant figure out the rest of the problems

3 Likes

Hey,

I tried to change it but still no changes. I don’t know why it is not working. Output does no have any errors

local CameraOffsetOn = Vector3.new(0, -50, 0)

local CameraOffsetOff = Vector3.new(0, 0, 0)

Answers pls…
No one has replied since April 9th…

Do this, on the tween service line, make a breakpoint on it to see if it is even running

1 Like

I changed the crouch script and now I found the problem but I don’t know how to solve it:

I have script which makes body parts visible on 1st person and when it’s not enabled, crouch works correctly but when this body visibility script is enabled, crouch is not working. What should I do??

body visibility script:

local character = (game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:wait())

game:GetService("RunService").RenderStepped:connect(function()
	if (character:FindFirstChild("RightUpperArm") and character:FindFirstChild("LeftUpperArm")) then
		character:FindFirstChild("RightUpperArm").LocalTransparencyModifier = 0

		character:FindFirstChild("RightLowerArm").LocalTransparencyModifier = 0

		character:FindFirstChild("RightHand").LocalTransparencyModifier = 0
		character:FindFirstChild("LeftUpperArm").LocalTransparencyModifier = 0

		character:FindFirstChild("LeftLowerArm").LocalTransparencyModifier = 0

		character:FindFirstChild("LeftHand").LocalTransparencyModifier = 0
		character:FindFirstChild("RightUpperLeg").LocalTransparencyModifier = 0

		character:FindFirstChild("RightLowerLeg").LocalTransparencyModifier = 0

		character:FindFirstChild("RightFoot").LocalTransparencyModifier = 0
		character:FindFirstChild("LeftUpperLeg").LocalTransparencyModifier = 0

		character:FindFirstChild("LeftLowerLeg").LocalTransparencyModifier = 0

		character:FindFirstChild("LeftFoot").LocalTransparencyModifier = 0
		character:FindFirstChild("UpperTorso").LocalTransparencyModifier = 0	

		character:FindFirstChild("LowerTorso").LocalTransparencyModifier = 0	
		character:FindFirstChild("Humanoid").CameraOffset = Vector3.new(0,0,-1)
	end
end)

New Crouch script:

wait(5)

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local crouchTweenInfo = TweenInfo.new(.2,Enum.EasingStyle.Quad,Enum.EasingDirection.Out)

local isCrouching = false

local crouchKeyCode = Enum.KeyCode.LeftShift


function crouch() 
	local humanoid = character:FindFirstChild("Humanoid")
	local goal = {}
	
	if humanoid then
		if isCrouching == false then
			goal.CameraOffset = Vector3.new(0,0,0)	
		elseif isCrouching == true then
			goal.CameraOffset = Vector3.new(0,-3,0)	
		end
	end	
	
	local crouchTween = TweenService:Create(humanoid,crouchTweenInfo,goal)
	crouchTween:Play()
end

UserInputService.InputBegan:connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == crouchKeyCode and isCrouching == false then
			isCrouching = true
			crouch()
		end
	end
end)

UserInputService.InputEnded:connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == crouchKeyCode and isCrouching == true then
			isCrouching = false
			crouch()
		end
	end
end)