--NOTE
--The animation can bug out, im still trying to find a way to fix that.


repeat wait() until game:IsLoaded()
wait(0.01)
local TweenService = game:GetService("TweenService") 
local tweenPart = game.Players.LocalPlayer.Character.Humanoid 

local info = TweenInfo.new(
	0.325,          
	Enum.EasingStyle.Back,  
	Enum.EasingDirection.Out,  
	0,                
	false,         
	0             
)
local info2 = TweenInfo.new(
	0.3,   
	Enum.EasingStyle.Back,   
	Enum.EasingDirection.Out,  
	0,               
	false,              
	0               
)

local Goals = {CameraOffset = Vector3.new(0,-1,0)}
local Goals2 = {CameraOffset = Vector3.new(0,0,0)}

local animsR6 = script.Parent.Parent.R6Anims
local animsR15 = script.Parent.Parent.R15Anims

local PartTween = TweenService:Create(tweenPart, info, Goals)
local PartTween2 = TweenService:Create(tweenPart, info2, Goals2)

local animidle = tweenPart:LoadAnimation(animsR15.Idle)
local animwalk = tweenPart:LoadAnimation(animsR15.Walk)

if tweenPart.RigType == Enum.HumanoidRigType.R6 then
	animidle = tweenPart:LoadAnimation(animsR6.Idle)
	animwalk = tweenPart:LoadAnimation(animsR6.Walk)
elseif tweenPart.RigType == Enum.HumanoidRigType.R15 then
	animidle = tweenPart:LoadAnimation(animsR15.Idle)
	animwalk = tweenPart:LoadAnimation(animsR15.Walk)
end

local crouching = false
local waiting = false

local function Crouch()
	if waiting == false then
		if crouching == false then
			script.Parent.Parent.ImageColor3 = script.Parent.Parent.Values.OnCrouchColor.Value
			script.Parent.Parent.CrouchImage.ImageColor3 = Color3.new(1, 1, 1)
			tweenPart.WalkSpeed = 8
			waiting = true
			crouching = true
			animidle:Play()
			PartTween:Play() --plays it
			tweenPart.Running:Connect(function(Speed)
				if crouching == true then
					if Speed >= 1 then
						animidle:Stop()
						if animwalk.IsPlaying == false then
							animwalk:Play()
						end

					else
						animidle:Play()
						animwalk:Stop()
					end
				end
			end)
			wait(0.05)
			waiting = false
		else
			script.Parent.Parent.ImageColor3 = script.Parent.Parent.Values.OriginalColor.Value
			script.Parent.Parent.CrouchImage.ImageColor3 = Color3.new(0, 0, 0)
			tweenPart.WalkSpeed = 16
			waiting = true
			crouching = false
			animidle:Stop()
			animwalk:Stop()
			PartTween2:Play()
			wait(0.05)
			waiting = false
		end
	end
end
local userinputservice = game:GetService("UserInputService")
userinputservice.InputBegan:Connect(function(input)
	if script.Parent.Parent.Visible == true then
		if input.KeyCode == Enum.KeyCode.C then
			Crouch()
		end
	end
end)
script.Parent.Parent.MouseButton1Click:Connect(function()
	Crouch()
end)

