You can write your topic however you want, but you need to answer these questions:
I’m working on a game and I’m trying to add the ability for the player to crouch
I made it so that the camera would tween down to make it seem like the player is crouching but when i crouch the camera tweens down and then snaps back to its original position
I tried looking for a solution online but i couldn’t find one
Here’s the script
--Services
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local plr = game.Players.LocalPlayer
local char = plr.Character
local Humanoid = char:FindFirstChild("Humanoid")
--Character Variables
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid") or character.Humanoid
--Crouch Variables
local crouchAnim = humanoid.Animator:LoadAnimation(script:WaitForChild("CrouchAnim"))
local isCrouched = false
--Settings
local crouchSpeed = 8
local walkSpeed = 12
--Pc Keybinds
local pcKeybind1 = Enum.KeyCode.C
local pcKeybind2 = Enum.KeyCode.C
--Xbox Keybinds
local xboxKeybind1 = Enum.KeyCode.ButtonB
local xboxKeybind2 = Enum.KeyCode.ButtonL2
local CameraInfo = TweenInfo.new(
0.3,
Enum.EasingStyle.Quint,
Enum.EasingDirection.Out,
0,
false,
0
)
local CrouchedGoal = {CameraOffset = Vector3.new(0,-1.75,0)}
local UncrouchedGoal = {CameraOffset = Vector3.new(0,0,0)}
local CrouchedTween = TweenService:Create(Humanoid, CameraInfo, CrouchedGoal)
local UncrouchedTween = TweenService:Create(Humanoid, CameraInfo, UncrouchedGoal)
--Adjust animation to match player speed
humanoid.Running:Connect(function(speed)
if speed > 0 then
crouchAnim:AdjustSpeed(1)
else --if not moving we stop animation
crouchAnim:AdjustSpeed(0)
end
end)
--Detect when crouch button activated
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.KeyCode == (pcKeybind1 or pcKeybind2) or input.KeyCode == (xboxKeybind1 or xboxKeybind2) then
if isCrouched == false then --Checks to see if crouched already
isCrouched = true --if not crouched we set crouched to true
character.PrimaryPart.CanCollide = false --makes HRP not collidable
CrouchedTween:Play()
crouchAnim:Play()
crouchAnim:AdjustSpeed(0)
humanoid.WalkSpeed = crouchSpeed
end
end
end)
--Detect when crouch button let go
UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.KeyCode == (pcKeybind1 or pcKeybind2) or input.KeyCode == (xboxKeybind1 or xboxKeybind2) then
if isCrouched then --Checks to see if crouched already
character.PrimaryPart.CanCollide = true
UncrouchedTween:Play()--makes HRP collidable
crouchAnim:Stop()
humanoid.WalkSpeed = walkSpeed
isCrouched = false --if crouched we set crouched to false
end
end
end)
--Stop crouch when player jumps
humanoid.Jumping:Connect(function()
if isCrouched then
crouchAnim:Stop()
character.PrimaryPart.CanCollide = true
humanoid.WalkSpeed = 14
isCrouched = false
end
end)
I also have a video
video