Crouch System help

I was making a crouch system and I need to know how to disable crouching when the player is climbing or sitting or swimming etc
here is my code

local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local Animation = script.Anims.Crouch
local Load1 = Humanoid:LoadAnimation(Animation)
local canDo = true
local crouched = false
local HRP = Character:WaitForChild("HumanoidRootPart")
local Player = game.Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local Button = PlayerGui.CrouchIcon.ImageButton
local Tweens = game:GetService("TweenService")
local info = TweenInfo.new(0.3)
local tween1 = Tweens:Create(Humanoid,info,{CameraOffset = Vector3.new(0,-1.5,0)})
local tween2 = Tweens:Create(Humanoid,info,{CameraOffset = Vector3.new(0,-1,0)})
UIS.InputBegan:Connect(function(A)
	if A.KeyCode == Enum.KeyCode.C  and canDo == true and crouched == false  then
		canDo = false
		Humanoid.JumpPower = 0
		Load1:Play()
		HRP:WaitForChild("Running").Volume = 0
		Humanoid.WalkSpeed = 9
		tween1:Play()
		wait(0.1)
		canDo = true
		crouched = true


	elseif A.KeyCode == Enum.KeyCode.C  and canDo == true and crouched == true then
		canDo = false
		Humanoid.JumpPower = 50
		Load1:Stop()
		Humanoid.WalkSpeed = 16
		tween2:Play()
		HRP:WaitForChild("Running").Volume = 0.5
		wait(0.1)
		canDo = true
		crouched = false
	end
end)
if UIS.KeyboardEnabled == false and UIS.MouseEnabled == false then
	PlayerGui.CrouchIcon.Enabled = true
	Button.MouseButton1Click:Connect(function(a)
		if canDo == true and crouched == false then
			canDo = false
			Humanoid.JumpPower = 0
			Load1:Play()
			Humanoid.WalkSpeed = 9
			HRP:WaitForChild("Running").Volume = 0
			tween1:Play()
			wait(0.1)
			canDo = true
			crouched = true
		elseif canDo == true and crouched == true then
			canDo = false
			Humanoid.JumpPower = 50
			Load1:Stop()
			Humanoid.WalkSpeed = 16
			tween2:Play()
			HRP:WaitForChild("Running").Volume = 0.5
			wait(0.1)
			canDo = true
			crouched = false

		end
	end)
end

	

first when I wanted to do this I made a function when the humanoid state is changed then added if condition to the function but it didnt work i will be appreciated for any help.

4 Likes

Try using humanoid:GetState(). This returns to current humanoid state.

1 Like

Sorry but am not that good at Humanoids can you give an example?

1 Like

I think this should work.

local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local Animation = script.Anims.Crouch
local Load1 = Humanoid:LoadAnimation(Animation)
local canDo = true
local crouched = false
local HRP = Character:WaitForChild("HumanoidRootPart")
local Player = game.Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local Button = PlayerGui.CrouchIcon.ImageButton
local Tweens = game:GetService("TweenService")
local info = TweenInfo.new(0.3)
local tween1 = Tweens:Create(Humanoid,info,{CameraOffset = Vector3.new(0,-1.5,0)})
local tween2 = Tweens:Create(Humanoid,info,{CameraOffset = Vector3.new(0,-1,0)})

Humanoid.StateChanged:Connect(function(old, new)
    if new ~= Enum.HumanoidStateType.Running or new ~= Enum.HumanoidStateType.Walking then
        canDo = false
    end
end

UIS.InputBegan:Connect(function(A)
	if A.KeyCode == Enum.KeyCode.C  and canDo == true and crouched == false  then
		canDo = false
		Humanoid.JumpPower = 0
		Load1:Play()
		HRP:WaitForChild("Running").Volume = 0
		Humanoid.WalkSpeed = 9
		tween1:Play()
		wait(0.1)
		canDo = true
		crouched = true


	elseif A.KeyCode == Enum.KeyCode.C  and canDo == true and crouched == true then
		canDo = false
		Humanoid.JumpPower = 50
		Load1:Stop()
		Humanoid.WalkSpeed = 16
		tween2:Play()
		HRP:WaitForChild("Running").Volume = 0.5
		wait(0.1)
		canDo = true
		crouched = false
	end
end)
if UIS.KeyboardEnabled == false and UIS.MouseEnabled == false then
	PlayerGui.CrouchIcon.Enabled = true
	Button.MouseButton1Click:Connect(function(a)
		if canDo == true and crouched == false then
			canDo = false
			Humanoid.JumpPower = 0
			Load1:Play()
			Humanoid.WalkSpeed = 9
			HRP:WaitForChild("Running").Volume = 0
			tween1:Play()
			wait(0.1)
			canDo = true
			crouched = true
		elseif canDo == true and crouched == true then
			canDo = false
			Humanoid.JumpPower = 50
			Load1:Stop()
			Humanoid.WalkSpeed = 16
			tween2:Play()
			HRP:WaitForChild("Running").Volume = 0.5
			wait(0.1)
			canDo = true
			crouched = false

		end
	end)
end

The Humanoid.StateChanged:Connect(function(old, new) checks when the humanoid’s state has changing, like if it switching from running to swimming, and checks if the new state is not running or walking, then in changes canDo to false.

1 Like

I tried this once but it didn’t work

2 Likes

did you see the error of my code?

2 Likes
if humanoid:GetState() == Enum.HumanoidStateType.Climbing then
return
end

On mobile so it might not look the best sorry. Also you have to add other humanoid state types too.

3 Likes

yes it had an error it says that walking is not a valid member of HumanoidStateType

1 Like

oh yea my bad, i need to remove walking.

1 Like
local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local Animation = script.Anims.Crouch
local Load1 = Humanoid:LoadAnimation(Animation)
local canDo = true
local crouched = false
local HRP = Character:WaitForChild("HumanoidRootPart")
local Player = game.Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local Button = PlayerGui.CrouchIcon.ImageButton
local Tweens = game:GetService("TweenService")
local info = TweenInfo.new(0.3)
local tween1 = Tweens:Create(Humanoid,info,{CameraOffset = Vector3.new(0,-1.5,0)})
local tween2 = Tweens:Create(Humanoid,info,{CameraOffset = Vector3.new(0,-1,0)})

Humanoid.StateChanged:Connect(function(old, new)
    if new ~= Enum.HumanoidStateType.Running then
        canDo = false
    end
end

UIS.InputBegan:Connect(function(A)
	if A.KeyCode == Enum.KeyCode.C  and canDo == true and crouched == false  then
		canDo = false
		Humanoid.JumpPower = 0
		Load1:Play()
		HRP:WaitForChild("Running").Volume = 0
		Humanoid.WalkSpeed = 9
		tween1:Play()
		wait(0.1)
		canDo = true
		crouched = true


	elseif A.KeyCode == Enum.KeyCode.C  and canDo == true and crouched == true then
		canDo = false
		Humanoid.JumpPower = 50
		Load1:Stop()
		Humanoid.WalkSpeed = 16
		tween2:Play()
		HRP:WaitForChild("Running").Volume = 0.5
		wait(0.1)
		canDo = true
		crouched = false
	end
end)
if UIS.KeyboardEnabled == false and UIS.MouseEnabled == false then
	PlayerGui.CrouchIcon.Enabled = true
	Button.MouseButton1Click:Connect(function(a)
		if canDo == true and crouched == false then
			canDo = false
			Humanoid.JumpPower = 0
			Load1:Play()
			Humanoid.WalkSpeed = 9
			HRP:WaitForChild("Running").Volume = 0
			tween1:Play()
			wait(0.1)
			canDo = true
			crouched = true
		elseif canDo == true and crouched == true then
			canDo = false
			Humanoid.JumpPower = 50
			Load1:Stop()
			Humanoid.WalkSpeed = 16
			tween2:Play()
			HRP:WaitForChild("Running").Volume = 0.5
			wait(0.1)
			canDo = true
			crouched = false

		end
	end)
end

Try now.

2 Likes

this one would work aswell, but don’t update everytime the state changes.

1 Like

the script work but i cant crouch any more i think i should add ‘else’

1 Like

Yep try that. I thought i wouldn’t add it cuz it might mess up with the other script lines. But lets see.

1 Like

This is true, but since it’s under user input service you don’t really need another function. You could also reset it before returning. You do have to move it outside though.

2 Likes

It worked! Thank you guys i really appreciate your help.

1 Like

Oh yea you are right. You’re way would make sense then.

2 Likes

Don’t forget to give the solution to @Mighty1964

1 Like

Thanks. You also showed me another way, so I will like all your posts.

1 Like

One more last question

local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local Animation = script.Anims.Crouch
local Load1 = Humanoid:LoadAnimation(Animation)
local load2 = Humanoid:LoadAnimation(script.Anims.CrouchWalk)
local canDo = true
local crouched = false
local HRP = Character:WaitForChild("HumanoidRootPart")
local Player = game.Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local Button = PlayerGui.CrouchIcon.ImageButton
local Tweens = game:GetService("TweenService")
local info = TweenInfo.new(0.3)
local tween1 = Tweens:Create(Humanoid,info,{CameraOffset = Vector3.new(0,-1.5,0)})
local tween2 = Tweens:Create(Humanoid,info,{CameraOffset = Vector3.new(0,-1,0)})
local HumanoidStatus = Humanoid:GetState()
local isWalking = false
print(HumanoidStatus)
Humanoid.StateChanged:Connect(function(old, new)
	if new ~= Enum.HumanoidStateType.Running then
		canDo = false
		
	else
		canDo = true
		
	end
	end)
UIS.InputBegan:Connect(function(A)
	if A.KeyCode == Enum.KeyCode.C  and canDo == true and crouched == false  then
		Humanoid.Running:connect(function(speed)
			if speed>0  then
				load2:Play()
			end
		end)
		Humanoid.WalkSpeed = 0
		Load1:Play()
		
		canDo = false
		
		Humanoid.Running:connect(function(speed) 
			if speed == 0 and crouched == true then 
				print(Player.Name.." has stopped walking.")
				
				Load1:Play()
				load2:Stop()
			elseif speed > 0 and crouched == true then 
				Load1:Stop()
				load2:Play()

				print(Player.Name.." is walking.")
			elseif crouched == false then
				Load1:Stop()
			load2:Stop()
			end
		end)
		Humanoid.JumpPower = 0
		

		
		HRP:WaitForChild("Running").Volume = 0
		tween1:Play()
		Humanoid.WalkSpeed = 9
		wait(0.1)
		canDo = true
		crouched = true
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)

	elseif A.KeyCode == Enum.KeyCode.C  and canDo == true and crouched == true then
		UIS.InputBegan:Connect(function(S)
			Humanoid.Running:connect(function(speed)
				if speed > 0 then
					load2:Stop()
					load2:Stop()
					load2:Stop()
					Load1:Stop()
				end
			end)
		end)
		
		canDo = false
		Load1:Stop()
		load2:Stop()
		Load1:Stop()
		load2:Stop()
		Humanoid.JumpPower = 50
		
		Humanoid.WalkSpeed = 16
		tween2:Play()
		HRP:WaitForChild("Running").Volume = 0.5
		wait(0.1)
		canDo = true
		crouched = false
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, true)
	end
end)
if UIS.KeyboardEnabled == false and UIS.MouseEnabled == false then
	PlayerGui.CrouchIcon.Enabled = true
	Button.MouseButton1Click:Connect(function(a)
		if canDo == true and crouched == false then
			canDo = false
			Player.Character.Humanoid.Running:connect(function(speed) 
				if speed == 0 then 
					print(Player.Name.." has stopped walking.")

					Load1:Play()
					load2:Stop()
				elseif speed > 0 then 
					Load1:Stop()
					load2:Play()

					print(Player.Name.." is walking.")
				end
			end)
			Humanoid.JumpPower = 0
			Humanoid.WalkSpeed = 9
			HRP:WaitForChild("Running").Volume = 0
			tween1:Play()
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)
			wait(0.1)
			canDo = true
			crouched = true
			
		elseif canDo == true and crouched == true then
			canDo = false
			Humanoid.JumpPower = 50
			Load1:Stop()
			load2:Stop()
			Humanoid.WalkSpeed = 16
				Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, true)
			tween2:Play()
			HRP:WaitForChild("Running").Volume = 0.5
			wait(0.1)
			canDo = true
			crouched = false

		end
	end)
end

	

the issue is when the player is walking after crouching it plays the crouchwalking animation

local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local Animation = script.Anims.Crouch
local Load1 = Humanoid:LoadAnimation(Animation)
local load2 = Humanoid:LoadAnimation(script.Anims.CrouchWalk)
local canDo = true
local crouched = false
local HRP = Character:WaitForChild("HumanoidRootPart")
local Player = game.Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local Button = PlayerGui.CrouchIcon.ImageButton
local Tweens = game:GetService("TweenService")
local info = TweenInfo.new(0.3)
local tween1 = Tweens:Create(Humanoid,info,{CameraOffset = Vector3.new(0,-1.5,0)})
local tween2 = Tweens:Create(Humanoid,info,{CameraOffset = Vector3.new(0,-1,0)})
local HumanoidStatus = Humanoid:GetState()
local isWalking = false
print(HumanoidStatus)
Humanoid.Running:Connect(function(speed)
if crouched == false then
load1:Stop()
load2:Stop()
end
end
Humanoid.StateChanged:Connect(function(old, new)
	if new ~= Enum.HumanoidStateType.Running then
		canDo = false
		
	else
		canDo = true
		
	end
	end)
UIS.InputBegan:Connect(function(A)
	if A.KeyCode == Enum.KeyCode.C  and canDo == true and crouched == false  then
		Humanoid.Running:connect(function(speed)
			if speed>0  then
				load2:Play()
			end
		end)
		Humanoid.WalkSpeed = 0
		Load1:Play()
		
		canDo = false
		
		Humanoid.Running:connect(function(speed) 
			if speed == 0 and crouched == true then 
				print(Player.Name.." has stopped walking.")
				
				Load1:Play()
				load2:Stop()
			elseif speed > 0 and crouched == true then 
				Load1:Stop()
				load2:Play()

				print(Player.Name.." is walking.")
			elseif crouched == false then
				Load1:Stop()
			load2:Stop()
			end
		end)
		Humanoid.JumpPower = 0
		

		
		HRP:WaitForChild("Running").Volume = 0
		tween1:Play()
		Humanoid.WalkSpeed = 9
		wait(0.1)
		canDo = true
		crouched = true
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)

	elseif A.KeyCode == Enum.KeyCode.C  and canDo == true and crouched == true then
		UIS.InputBegan:Connect(function(S)
			Humanoid.Running:connect(function(speed)
				if speed > 0 then
					load2:Stop()
					load2:Stop()
					load2:Stop()
					Load1:Stop()
				end
			end)
		end)
		
		canDo = false
		Load1:Stop()
		load2:Stop()
		Load1:Stop()
		load2:Stop()
		Humanoid.JumpPower = 50
		
		Humanoid.WalkSpeed = 16
		tween2:Play()
		HRP:WaitForChild("Running").Volume = 0.5
		wait(0.1)
		canDo = true
		crouched = false
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, true)
	end
end)
if UIS.KeyboardEnabled == false and UIS.MouseEnabled == false then
	PlayerGui.CrouchIcon.Enabled = true
	Button.MouseButton1Click:Connect(function(a)
		if canDo == true and crouched == false then
			canDo = false
			Player.Character.Humanoid.Running:connect(function(speed) 
				if speed == 0 then 
					print(Player.Name.." has stopped walking.")

					Load1:Play()
					load2:Stop()
				elseif speed > 0 then 
					Load1:Stop()
					load2:Play()

					print(Player.Name.." is walking.")
				end
			end)
			Humanoid.JumpPower = 0
			Humanoid.WalkSpeed = 9
			HRP:WaitForChild("Running").Volume = 0
			tween1:Play()
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)
			wait(0.1)
			canDo = true
			crouched = true
			
		elseif canDo == true and crouched == true then
			canDo = false
			Humanoid.JumpPower = 50
			Load1:Stop()
			load2:Stop()
			Humanoid.WalkSpeed = 16
				Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, true)
			tween2:Play()
			HRP:WaitForChild("Running").Volume = 0.5
			wait(0.1)
			canDo = true
			crouched = false

		end
	end)
end
1 Like