Currently Attempting to make a Crouch/Idle but new to scripting

Currently I’ve been trying for a couple of hours now to make an Idle Crouch + Walking Crouch script where when the player is Idle it will perform a different animation from when they are moving while in the crouch position.

I have achieved making the player able to perform the crouch animation but don’t understand how to use Humanoid.Running and Humanoid.MoveDirection and also where I should do this within the script.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")

local Animation = Instance.new("Animation")
Animation.Parent = player.Character

local Animate
local debounce = false

UIS.InputBegan:Connect(function(Input)
	if Humanoid.MoveDirection.Magnitude < 0 then	
		Animation.AnimationId = "rbxassetid://6299885626"
	else 
		Animation.AnimationId = "rbxassetid://0"
		if Input.KeyCode == Enum.KeyCode.LeftControl then
			if not debounce then
				debounce = true
				Animate = Humanoid:LoadAnimation(Animation)
				Animate:Play()
				Humanoid.WalkSpeed = Humanoid.WalkSpeed - 9
				Humanoid.JumpPower = 0
			else
				Animate:Stop()
				Humanoid.WalkSpeed = 16
				Humanoid.JumpPower = 50

				debounce = false
			end  
		end
	end
end) 

Edit, so I worked out how to Loop the change but will me looping the Idle check in the background effect the performance of my game?

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")

local Animation = Instance.new("Animation")
Animation.Parent = player.Character

local Animate

local debounce = false
	UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.C then
		if not debounce then
			debounce = true
				Animate = Humanoid:LoadAnimation(Animation)
				Animate:Play()
				Humanoid.WalkSpeed = Humanoid.WalkSpeed - 9
				Humanoid.JumpPower = 0		
			else
				Animate:Stop()
				Humanoid.WalkSpeed = 16
				Humanoid.JumpPower = 50

				debounce = false
		end
	end	
end)

while true do 
	wait(0.1)
	if Humanoid.MoveDirection.Magnitude == 0 then
		Animation.AnimationId = "rbxassetid://8365991997"
		print("Idle")
	else
		Animation.AnimationId = "rbxassetid://8365991997"
		print("Walking")
	end
end

Updated my script to this but the animations don’t work now and I’m still very confused, could I get some help please?

-- Services
local CAS = game:GetService("ContextActionService")
local UIS = game:GetService("UserInputService")


-- Player Related
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")


--Values
local bool1 = script:WaitForChild("Bool")
local bool = bool1.Value

local bool2 = script:WaitForChild("Anim")
local Anim = bool2.Value


--Animations

local anim1 = Instance.new("Animation")
anim1.Parent = player.Character
anim1.Name = "CrouchIdle"
anim1.AnimationId = ("rbxassetid://8367664236")

local anim2 = Instance.new("Animation")
anim2.Parent = player.Character
anim2.Name = "CrouchWalk"
anim2.AnimationId = ("rbxassetid://8365991997")

local a1 = script.Parent:WaitForChild("CrouchIdle")
local a2 = script.Parent:WaitForChild("CrouchWalk")

local animate1 = Humanoid:LoadAnimation(anim1)
local animate2 = Humanoid:LoadAnimation(anim2)

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.C then -- Keybind (Set to C)
		if not bool == true then
			bool = true
			Humanoid.WalkSpeed = Humanoid.WalkSpeed - 9 --Sets Walkspeed
			Humanoid.JumpPower = 0 --Sets Jump Power
		else
			bool = false
			Humanoid.WalkSpeed = 16 --Sets Walkspeed
			Humanoid.JumpPower = 50 --Sets Jump Power
		end
	end	
end)

Humanoid.Running:Connect(function(speed)
	if speed > 0 then
		print("Walking, ",Anim)
		Anim = true
		Humanoid:SetAttribute("Idle", nil)
		Humanoid:SetAttribute("Walking", true)
	else 
		Humanoid:SetAttribute("Idle", true)
		Humanoid:SetAttribute("Walking", nil)
		print("Idle, ",Anim)
		Anim = false
	end
end)

Humanoid:GetAttributeChangedSignal("Idle"):Connect(function()
	if (bool == true and Anim == false and animate1:Stop()) then
		animate1:Play()
	else if (bool == true and Anim == true and animate1:Play()) then
			animate1:Stop()
		else 
			animate1:Stop()
		end
	end	
end)

Humanoid:GetAttributeChangedSignal("Walking"):Connect(function()
	if (bool == true and Anim == true and animate2:Stop()) then
		animate2:Play()
	else if (bool == true and Anim == false and animate2:Play()) then
			animate2:Stop()			
		else
			animate2:Stop()	
		end
	end	
end)
2 Likes