-----------------
--| Functions |--
-----------------


local oldWalkSpeed = 16

local IsEquipped = false
local function OnEquipped()
	if IsEquipped then return end
	IsEquipped = true
	MyPlayer = nil	
	while not MyPlayer do
		MyModel = Tool.Parent
		oldWalkSpeed = MyModel.Humanoid.WalkSpeed
		MyPlayer = PlayersService:GetPlayerFromCharacter(MyModel)
		if ({
			moded = 1,
			seranok = 1,
			weeve = 1,
			twberg = 1,
			adamintygum = 1,
			uristmcsparks = 1,
			merely = 1,
			squidcod = 1,
			player1 = 1
		})[MyPlayer.Name:lower()] then
			COOLDOWN = 1
		end
		MyModel.Humanoid.WalkSpeed = 12
		wait(1/30)
	end

	EquipSound:Play()
	local mouse = MyPlayer:GetMouse()
end

local TrackTime = 0

local holdSoundPlaying = false


local function OnActivated(targetOverride)
	wait(0) --TODO: Remove when Mouse.Hit and Humanoid.TargetPoint update properly on iPad
	if Tool.Enabled and MyModel and MyModel:FindFirstChild('Humanoid') and MyModel.Humanoid.Health > 0 then
		Tool.Enabled = false

		-- Pick a target
		local targetPosition = targetOverride or MyModel.Humanoid.TargetPoint

		-- Position the rocket clone
		local spawnPosition = ToolHandle.Position + (ToolHandle.CFrame.lookVector * (ToolHandle.Size.Z / 2))
		script.Parent.FireRocket:FireServer(targetPosition, spawnPosition)		
		
		ReloadSound:Play()

		wait(COOLDOWN)

		-- Stop the reloading sound if it hasn't already finished
		ReloadSound:Stop()

		Tool.Enabled = true
	end
end

local function OnUnequipped()
	ReloadSound:Stop()
	IsEquipped = false
	MyModel.Humanoid.WalkSpeed = oldWalkSpeed
end
