hello im making a crouch script and its not really my specialty for some reason so i would just ask for help since it annoys me because it won’t work so if someone could fix it for me that would be nice.
-- Services
local UIS = game:GetService("UserInputService")
-- Variables
local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local humanoid = Character:WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator")
-- Animations
local walkAnim = Instance.new("Animation")
walkAnim.AnimationId = "rbxassetid://11064740474"
local idleAnim = Instance.new("Animation")
idleAnim.AnimationId = "rbxassetid://11064503891"
-- Animation Tracks
local idleTrack = Animator:LoadAnimation(idleAnim)
idleTrack.Looped = false
local walkTrack = Animator:LoadAnimation(idleAnim)
walkTrack.Looped = false
local crouched = false
local moving = false
UIS.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then
return
end
if input.KeyCode == Enum.KeyCode.LeftControl or Enum.KeyCode.C then
if not crouched then
if not moving then
crouched = true
idleTrack:Play()
idleTrack.Looped = true
else
crouched = true
walkTrack:Play()
walkTrack.Looped = true
end
end
end
end)
-- Services
local UIS = game:GetService("UserInputService")
-- Variables
local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local humanoid = Character:WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator")
-- Animations
local walkAnim = Instance.new("Animation")
walkAnim.AnimationId = "rbxassetid://11064740474"
local idleAnim = Instance.new("Animation")
idleAnim.AnimationId = "rbxassetid://11064503891"
-- Animation Tracks
local idleTrack = Animator:LoadAnimation(idleAnim)
idleTrack.Looped = false
local walkTrack = Animator:LoadAnimation(idleAnim)
walkTrack.Looped = false
local crouched = false
local moving = false
UIS.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then
return
end
if input.KeyCode == Enum.KeyCode.LeftControl or input.KeyCode == Enum.KeyCode.C then
if not crouched then
if not moving then
crouched = true
Idle()
else
crouched = true
Walk()
end
else
if not moving then
crouched = false
Idle()
else
crouched = false
Walk()
end
end
end
end)
-- Functions
function Walk()
walkTrack:Play()
walkTrack.Looped = true
end
function Idle()
idleTrack:Play()
idleTrack.Looped = true
end
Hey, saw this post so I tried my hand at making a very basic crouching script. Let me know if you run into any problems.
--CheddarRius was here
-- Setting up services
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local RS = game:GetService("RunService")
--Setting up vars
local plr = Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
-- The player is not crouched by default
local isCrouched = false
--loading idle crouch animation
local idleAnim = Instance.new("Animation")
idleAnim.AnimationId = "rbxassetid://YOUR_ANIMATION_ID"
local idleTrack = hum.Animator:LoadAnimation(idleAnim)
idleTrack.Looped = true
--Set idle animation priority if you need:
--idleTrack.Priority = Enum.AnimationPriority.YOUR_ANIMATION_PRIORITY
--loading moving crouch animation
local moveAnim = Instance.new("Animation")
moveAnim.AnimationId = "rbxassetid://YOUR_ANIMATION_ID"
local moveTrack = hum.Animator:LoadAnimation(moveAnim)
moveTrack.Looped = true
--Set move animation priority if you need:
--moveTrack.Priority = Enum.AnimationPriority.YOUR_ANIMATION_PRIORITY
-- Handling idle crouching
UIS.InputBegan:Connect(function(input, gameprocessed)
-- Check if the input was processed by another script
if gameprocessed then
return
end
-- Check if the input is for crouching
if input.KeyCode == Enum.KeyCode.C or input.KeyCode == Enum.KeyCode.LeftControl then
if not isCrouched then
-- Crouch
isCrouched = true
hum.WalkSpeed = YOUR_CROUCH_SPEED -- Set custom crouch speed
hum.JumpHeight = YOUR_CROUCH_JUMP_HEIGHT -- Set custom crouch jump height
idleTrack:Play(.3)
else
-- Stand up
isCrouched = false
hum.WalkSpeed = YOUR_WALK_SPEED -- Set default walking speed (default is 16)
hum.JumpHeight = YOUR_JUMP_HEIGHT --Set default jump height (default is 5)
idleTrack:Stop(.3)
moveTrack:Stop(.3)
end
end
end)
-- Handling crouch moving
RS.Heartbeat:Connect(function()
if isCrouched and hum.MoveDirection.Magnitude > 0.1 and not hum.Sit and hum.FloorMaterial ~= Enum.Material.Air then
-- Player is moving while crouched
if not moveTrack.IsPlaying then
moveTrack:Play(.4)
end
else
-- Player is not moving or not crouched
if moveTrack.IsPlaying then
moveTrack:Stop(.4)
end
end
end)
--If your animations are children of the script (Put instead of line 16-26) (Ignore if not):
--[[
local idleAnim = script.YOUR_ANIMATION_NAME
local idleTrack = hum.Animator:LoadAnimation(idleAnim)
idleAnim.Looped = true
idleTrack.Priority = Enum.AnimationPriority.YOUR_ANIMATION_PRIORITY
local moveIdle = script.YOUR_ANIMATION_NAME
local moveAnim = hum.Animator:LoadAnimation(moving)
moveAnim.Looped = true
moveTrack.Priority = Enum.AnimationPriority.YOUR_ANIMATION_PRIORITY
]]