-- General settings

local COOLDOWN = 1
local THROW_SPEED = 100

local tool = script.Parent

local anim
local toolPlr
local debounce = false

script.ClientThrow.OnServerEvent:connect(function(plr)
	
	-- Check if tool is equipped and correct player
	if not debounce and tool.Parent ~= nil and tool.Parent.ClassName ~= "Backpack" and plr == toolPlr then
		debounce = true
		
		-- Play animation
		anim:Play()
		
	end
	
end)

tool.Equipped:connect(function()
	
	-- Set tool player
	toolPlr = game.Players:GetPlayerFromCharacter(tool.Parent)
	
	-- Load animation
	anim = tool.Parent.Humanoid:LoadAnimation(script.Animation)
	anim.KeyframeReached:connect(function(keyframe)
		
		-- Check what frame
		if keyframe == "Throw" then
			
			-- Create snowball
			game.ReplicatedStorage.Interactions.Client.CreateSnowball:FireAllClients(toolPlr.Character, tool.Handle, (script.GetMouseHit:InvokeClient(toolPlr) - script.Parent.Handle.Position).Unit * THROW_SPEED)
			
			-- Play sound
			tool.Handle.Sound:Play()
			
		end
		
	end)
	anim.Stopped:connect(function()
		debounce = false
	end)
	
	-- Reset just in case
	debounce = false
	
end)

tool.Unequipped:connect(function()
	
	-- Stop animation
	anim:Stop()
	anim:destroy()
	
end)
