-- FUNCTIONS --

Tool.Equipped:Connect(function()
	Character = Tool.Parent
	HRP = Character:WaitForChild("HumanoidRootPart")
	Humanoid = Character:WaitForChild("Humanoid")
	
	equippedIdle = Humanoid:LoadAnimation(script.Animations.Idle)
	equippedIdle:Play()
	Equipped.Value = true
	
	attackRemote:FireServer("Unblock", Tool.Name)
	
	if not childAdded then
		childAdded = Character.ChildAdded:Connect(function()
			if not Equipped.Value then return end
			if not Player:FindFirstChild("Ragdoll") then
				equippedIdle:Play()
			else
				equippedIdle:Stop()
			end
		end)
	end

	if not childRemoved then
		childRemoved = Character.ChildRemoved:Connect(function()
			if not Equipped.Value then return end
			if not Player:FindFirstChild("Ragdoll") then
				equippedIdle:Play()
			else
				equippedIdle:Stop()
			end
		end)
	end
end)

Tool.Unequipped:Connect(function()
	equippedIdle:Stop()
	attackRemote:FireServer("Unblock", Tool.Name)
	
	Equipped.Value = false
	
	if childAdded ~= nil then
		childAdded:Disconnect()
		childAdded = nil
	end

	if childRemoved ~= nil then
		childRemoved:Disconnect()
		childRemoved = nil
	end
end)

UIS.InputBegan:Connect(function(Input, isTyping)
	if isTyping then return end

	if Input.UserInputType == Enum.UserInputType.MouseButton1 then
		if not Humanoid then return end
		if Humanoid.Health > 0 then
			local Disabled = Character:FindFirstChild("Disabled")
			if not Disabled then
				if UIS:IsKeyDown(Enum.KeyCode.Space) then
					attackRemote:FireServer("M1", Tool.Name, true)
				else
					attackRemote:FireServer("M1", Tool.Name, false)
				end
				coroutine.wrap(function()
					local noJumpValue = Instance.new("BoolValue")
					noJumpValue.Name = "noJump"
					noJumpValue.Parent = Character
					game.Debris:AddItem(noJumpValue, 1)
				end)()
			end
		end
	elseif Input.KeyCode == Enum.KeyCode.F then
		if not Humanoid then return end
		if Humanoid.Health > 0 then
			local Disabled = Character:FindFirstChild("Disabled")
			if not Disabled then
				attackRemote:FireServer("Block", Tool.Name)
			end
		end
	end
end)

UIS.InputEnded:Connect(function(Input, isTyping)
	if isTyping then return end
	
	if Input.KeyCode == Enum.KeyCode.F then
		attackRemote:FireServer("Unblock", Tool.Name)
	end
end)
