local humanoid = script.Parent:WaitForChild("Humanoid")
local animationIdle = humanoid:LoadAnimation(script:WaitForChild("CrouchIdle"))
local animationMoving = humanoid:LoadAnimation(script:WaitForChild("CrouchMoving"))
local crouching = false
uis.InputBegan:Connect(function(inp, processed)
if processed then return end
if inp.KeyCode == Enum.KeyCode.C then
crouching = not crouching
end
end)
game:GetService("RunService").Heartbeat:Connect(function()
script.Parent.HumanoidRootPart.CanCollide = false
if crouching then
humanoid.WalkSpeed = 1
animationIdle:Play()
if humanoid.MoveDirection.Magnitude > 0 then
if not animationMoving.IsPlaying then animationMoving:Play() end
else
animationMoving:Stop()
end
else
animationIdle:Stop()
animationMoving:Stop()
humanoid.WalkSpeed = 16
end
end)
The script is still gonna be playing the Idle animation when the player is moving so move the animationIdle:Play() line of code to where you stop the moving animation when the player is not moving
and then to stop the idle animation playing when the player starts moving put animationIdle:Stop() line of code in where you start the moving animation when the player starts moving
Change these lines
animationIdle:Play()
if humanoid.MoveDirection.Magnitude > 0 then
if not animationMoving.IsPlaying then animationMoving:Play() end
else
animationMoving:Stop()
end
To these
if humanoid.MoveDirection.Magnitude > 0 then
if not animationMoving.IsPlaying then animationMoving:Play() end
animationIdle:Stop()
else
animationMoving:Stop()
animationIdle:Play()
end
local Deb = false
local function CheckAlive()
local Hum = script.Parent:FindFirstChild("Humanoid")
local Head = script.Parent:FindFirstChild("Head")
local HumanRoot = script.Parent:FindFirstChild("HumanoidRootPart")
if Hum and Hum.Health > 0 and Head and HumanRoot then
return true
else
return false
end
end
local function CheckHead()
if not CheckAlive() then return end
local rayOrigin = script.Parent.Head.Position
local rayDirection = Vector3.new(0, 2, 0)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
if hitPart.Transparency == 0 then
return false
else
return true
end
else
return true
end
end
function Crouch(actionName, inputState, inputObject)
if not CheckAlive() then return end
if inputState == Enum.UserInputState.Begin then
if not Deb then
Deb = true
game:GetService("TweenService"):Create(script.Parent:FindFirstChild("Humanoid"), TweenInfo.new(0.2), {HipHeight = 0.3}):Play()
else
if CheckHead() then
Deb = false
game:GetService("TweenService"):Create(script.Parent:FindFirstChild("Humanoid"), TweenInfo.new(0.2), {HipHeight = 2}):Play()
end
end
end
end
game:GetService("ContextActionService"):BindAction("Crouch", Crouch, true, Enum.KeyCode.LeftControl, Enum.KeyCode.C, Enum.KeyCode.ButtonB)
Then just add the animations. If you need help with that let me know and I can help.