local crouchKey = Enum.KeyCode.C local crouchSpeed = 8 local normalSpeed = 16 local…
- User
local crouchKey = Enum.KeyCode.C local crouchSpeed = 8 local normalSpeed = 16 local crouchHeight = 1.5 local normalHeight = 2
local UIS = game:GetService("UserInputService") local Players = game:GetService("Players")
local player = Players.LocalPlayer local character = player.Character or player.CharacterAdded:Wait() local humanoid = character:WaitForChild("Humanoid") local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local crouchIdleAnimation = humanoid:LoadAnimation(script.CrouchIdle) local crouchWalkAnimation = humanoid:LoadAnimation(script.CrouchWalk)
local isCrouching = false local isMoving = false
local function crouch() isCrouching = true humanoid.WalkSpeed = crouchSpeed humanoidRootPart.Size = Vector3.new(humanoidRootPart.Size.X, crouchHeight, humanoidRootPart.Size.Z) if isMoving then crouchIdleAnimation:Stop() crouchWalkAnimation:Play() else crouchWalkAnimation:Stop() crouchIdleAnimation:Play() end end
local function stand() isCrouching = false humanoid.WalkSpeed = normalSpeed humanoidRootPart.Size = Vector3.new(humanoidRootPart.Size.X, normalHeight, humanoidRootPart.Size.Z) crouchIdleAnimation:Stop() crouchWalkAnimation:Stop() end
humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function() if humanoid.MoveDirection.Magnitude > 0 then isMoving = true if isCrouching then crouchIdleAnimation:Stop() if not crouchWalkAnimation.IsPlaying then crouchWalkAnimation:Play() end end else isMoving = false if isCrouching then crouchWalkAnimation:Stop() if not crouchIdleAnimation.IsPlaying then crouchIdleAnimation:Play() end end end end)
UIS.InputBegan:Connect(function(input) if input.KeyCode == crouchKey and not isCrouching then crouch() end end)
UIS.InputEnded:Connect(function(input) if input.KeyCode == crouchKey and isCrouching then stand() end end)
how can you improve this?
- Assistant