Hello everyone, i tried to make a crouching script for my stealth game, i made it into a module function and the idea was pretty simple:
when you press shift, if you’re crouched (when the boolean is true) you stand up, if not, (the crouching boolean is false) you enter in crouch.
The main issue is that, after the first if statement’s else, the code runs and removes the crouching animation, but after you start moving your character returns into crouching.
here’s the code:
function PlayerSettings.Crouch(player1)
local Character = player1.Character
local Humanoid = Character.Humanoid
local Animate = Character.Animate
local ProprietyChanged = nil
local AnimIdle = nil
local Anim = nil
local Animator1 = nil
local Animator2 = nil
if not Library.GENERAL.Crouching.boolean then
Library.GENERAL.Crouching.boolean = true
local AnimIdle = Instance.new("Animation")
AnimIdle.AnimationId = Library.GENERAL.Crouching.CrouchingIdle
AnimIdle.Name = "CrouchIdle"
local Anim = Instance.new("Animation")
Anim.AnimationId = Library.GENERAL.Crouching.CrouchingAnim
Anim.Name = "Crouching"
local Animator1 = Humanoid.Animator:LoadAnimation(AnimIdle)
local Animator2 = Humanoid.Animator:LoadAnimation(Anim)
Animator1:Play()
ProprietyChanged = Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
if Humanoid.MoveDirection.Magnitude > 0 then
if Library.GENERAL.Crouching.Walking then return end
if Animator1 ~= nil then Animator1:Stop() end
Animator2:Play()
Library.GENERAL.Crouching.Idle = false
Library.GENERAL.Crouching.Walking = true
else
if Library.GENERAL.Crouching.Idle then return end
if Animator2 ~= nil then Animator2:Stop() end
Animator1:Play()
Library.GENERAL.Crouching.Idle = true
Library.GENERAL.Crouching.Walking = false
end
end)
else -- **This is the part about standing up**
print("Executed")
if ProprietyChanged ~= nil then ProprietyChanged:Disconnect() ProprietyChanged = nil end
if Animator1 ~= nil then Animator1:Stop() Animator1:Destroy() end
if Animator2 ~= nil then Animator2:Stop() Animator2:Destroy() end
if Anim ~= nil then Anim:Destroy() end
if AnimIdle ~= nil then AnimIdle:Destroy() end
Library.GENERAL.Crouching.boolean = false
Library.GENERAL.Crouching.Walking = false
Library.GENERAL.Crouching.Idle = false
end
end