--[[
	
	tweenJoint Function Parameters:
	
	Object Joint - This has to be a weld with a C0 and C1 property
	
	CFrame newC0 - This is what the new C0 of the weld will be. You can put nil if you don't want to effect the C0
	
	CFrame newC1 - This is what the new C1 of the weld will be. You can put nil if you don't want to effect the C1
	
	function Alpha - This is an alpha function that takes an input parameter of a number between 0 and 90 and returns a number between 0 and 1.
		For example, function(X) return math.sin(math.rad(X)) end
		
	float Duration - This is how long the tweening takes to complete
	
--]]


local RS = game:GetService("RunService")

local Proned2

Rendido.Changed:Connect(function()
	Stance:FireServer(Stances,Virar,Rendido.Value)
end)

Algemado.Changed:Connect(function()
	Stance:FireServer(Stances,Virar,Rendido.Value)
end)

function Stand()
	
	Stance:FireServer(Stances,Virar,Rendido.Value)
	TS:Create(char.Humanoid, TweenInfo.new(.3), {CameraOffset = Vector3.new(CameraX,CameraY,0)} ):Play()
	
	if Steady then
		Humanoid.WalkSpeed = ServerConfig.SlowPaceWalkSpeed * StancesPasta.Mobility.Value
		Humanoid.JumpPower = ServerConfig.JumpPower
		else
		if  Ferido.Value == false then
			Humanoid.WalkSpeed = ServerConfig.NormalWalkSpeed * StancesPasta.Mobility.Value
			Humanoid.JumpPower = ServerConfig.JumpPower
		else
			Humanoid.WalkSpeed = ServerConfig.SlowPaceWalkSpeed * StancesPasta.Mobility.Value
			Humanoid.JumpPower = ServerConfig.JumpPower
		end
	end


	IsStanced = false	
	Proned2 = Vector3.new(0,0,0)

end





function Crouch()
	
	Stance:FireServer(Stances,Virar,Rendido.Value)
	TS:Create(char.Humanoid, TweenInfo.new(.3), {CameraOffset = Vector3.new(CameraX,CameraY,0)} ):Play()
	Humanoid.WalkSpeed = ServerConfig.CrouchWalkSpeed * StancesPasta.Mobility.Value
	Humanoid.JumpPower = 0
	IsStanced = true	
	Proned2 = Vector3.new(0,0,0)
end

function Prone()
	
	Stance:FireServer(Stances,Virar,Rendido.Value)
	TS:Create(char.Humanoid, TweenInfo.new(.3), {CameraOffset = Vector3.new(CameraX,CameraY,0)} ):Play()
	if Rendido.Value == true then
	Humanoid.WalkSpeed = 0
	else
	Humanoid.WalkSpeed = ServerConfig.ProneWalksSpeed * StancesPasta.Mobility.Value
	end
	Humanoid.JumpPower = 0 
	IsStanced = true
	Proned2 = Vector3.new(0,0.5,0.5)
end

function Lean()

TS:Create(char.Humanoid, TweenInfo.new(.3), {CameraOffset = Vector3.new(CameraX,CameraY,0)} ):Play()
Stance:FireServer(Stances,Virar,Rendido.Value)
end
	
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.KeyCode == Enum.KeyCode.C and Stances == 0 and ChangeStance and Caido.Value == false and Correndo.Value == false and Rappeling.Value == false and rodeath.Value == false and not _G.Lean and not Sentado then
			Stances = 1
			--CameraX = 0
			CameraY = -1
			Poses.Levantado.Visible = false
			Poses.Agaixado.Visible = true
			Poses.Deitado.Visible = false
			Crouch()
			
				
			_G.Crouched = true
		elseif input.KeyCode == Enum.KeyCode.C and Stances == 1 and ChangeStance and Caido.Value == false and Correndo.Value == false and Rappeling.Value == false and not _G.Lean and not Sentado then	
			Stances = 2
			CameraX = 0
			CameraY = -3.25
			Virar = 0
			Poses.Levantado.Visible = false
			Poses.Agaixado.Visible = false
			Poses.Deitado.Visible = true
			Poses.Esg_Right.Visible = false
			Poses.Esg_Left.Visible = false
			Lean()
			Prone()
			
			_G.Crouched = false
			_G.Proned = true
			
		elseif input.KeyCode == Enum.KeyCode.X and Stances == 2 and ChangeStance and Caido.Value == false and Correndo.Value == false and Rappeling.Value == false and rodeath.Value == false and not _G.Lean and not Sentado  then
			_G.Crouched = true
			_G.Proned = false
			Stances = 1
			--CameraX = 0
			CameraY = -1
			Poses.Levantado.Visible = false
			Poses.Agaixado.Visible = true
			Poses.Deitado.Visible = false
			Crouch()
			
				
		elseif input.KeyCode == Enum.KeyCode.X and Stances == 1 and ChangeStance and Caido.Value == false and Correndo.Value == false and Rappeling.Value == false and rodeath.Value == false and not _G.Lean and not Sentado  then		
			_G.Crouched = false
			Stances = 0
			--CameraX = 0
			CameraY = 0
			Poses.Levantado.Visible = true
			Poses.Agaixado.Visible = false
			Poses.Deitado.Visible = false
			Stand()
		elseif input.KeyCode == Enum.KeyCode.Z and ChangeStance and Caido.Value == false and Correndo.Value == false and rodeath.Value == false then
				if not Steady then
				Steady = true
				Poses.Steady.Visible = true							
					if Stances == 0 then
						Stand()
						Humanoid.WalkSpeed = ServerConfig.SlowPaceWalkSpeed * StancesPasta.Mobility.Value
					end
				else
				Steady = false
				Poses.Steady.Visible = false
					if Stances == 0 then
						Stand()
						Humanoid.WalkSpeed = ServerConfig.NormalWalkSpeed * StancesPasta.Mobility.Value
					end
				end
		elseif input.KeyCode == Enum.KeyCode.E and Stances ~= 2 and ChangeStance and Caido.Value == false and Correndo.Value == false and rodeath.Value == false and not _G.Lean and CanLean then		
			if Virar == 0 or Virar == -1 then
				Virar = 1
				CameraX = 1.75
				Poses.Esg_Right.Visible = true
				Poses.Esg_Left.Visible = false
			else
				Virar = 0
				CameraX = 0
				Poses.Esg_Right.Visible = false
				Poses.Esg_Left.Visible = false
			end
			Lean()
		elseif input.KeyCode == Enum.KeyCode.Q and Stances ~= 2 and ChangeStance and Caido.Value == false and Correndo.Value == false and rodeath.Value == false and not _G.Lean and CanLean  then
			if Virar == 0 or Virar == 1 then
				Virar = -1
				CameraX = -1.75
				Poses.Esg_Right.Visible = false
				Poses.Esg_Left.Visible = true
			else
				Virar = 0
				CameraX = 0
				Poses.Esg_Right.Visible = false
				Poses.Esg_Left.Visible = false
			end
			Lean()
		elseif input.KeyCode == Enum.KeyCode.LeftShift and Correndo.Value == false and Ferido.Value == false and Caido.Value == false and Rappeling.Value == false and rodeath.Value == false and not _G.Lean and not Sentado and not cansado then	
			if Velocidade > 0 then
			ChangeStance = false
			Correndo.Value = true
			--Steady = false
			Stand()
			Stances = 0
			Virar = 0
			CameraX = 0
			CameraY = 0
			Lean()
			Poses.Levantado.Visible = true
			Poses.Agaixado.Visible = false
			Poses.Deitado.Visible = false
			Poses.Esg_Right.Visible = false
			Poses.Esg_Left.Visible = false
			Humanoid.WalkSpeed = ServerConfig.RunWalkSpeed * StancesPasta.Mobility.Value
			end
			end
		end
	end)
	
UserInputService.InputEnded:Connect(function(input, gameProcessed)
if not gameProcessed then
	if input.KeyCode == Enum.KeyCode.LeftShift and Correndo.Value == true and not _G.Lean then	
		ChangeStance = true
		Correndo.Value = false
		if Steady then
		Humanoid.WalkSpeed = ServerConfig.SlowPaceWalkSpeed * StancesPasta.Mobility.Value
		Poses.Steady.Visible = true	
		else
		Humanoid.WalkSpeed = ServerConfig.NormalWalkSpeed * StancesPasta.Mobility.Value
		Poses.Steady.Visible = false
		end
		end
	end
end)


	
Stand()

Humanoid.Died:Connect(function()
	TS:Create(char.Humanoid, TweenInfo.new(1), {CameraOffset = Vector3.new(0,0,0)} ):Play()
	ChangeStance = false
	Main.Visible = false
end)

Humanoid.Seated:Connect(function(IsSeated)
local Seat = Humanoid.SeatPart
    if IsSeated  then
        Sentado = true
        Stand()
        Stances = 0
        Virar = 0
        CameraX = 0
        CameraY = 0
        Lean()
        Poses.Levantado.Visible = true
        Poses.Agaixado.Visible = false
        Poses.Deitado.Visible = false
        Poses.Esg_Right.Visible = false
        Poses.Esg_Left.Visible = false

        if Seat:IsA("VehicleSeat") or ("Seat") then
            CanLean = false
        end

    else
        Sentado = false
        CanLean = true
    end
end)

Humanoid.Running:Connect(function(Speed)