-- Function to bind to equip event

local function equip()
	local torso = player.Character.Torso
	
	-- Setup joint variables
	neck = torso.Neck	
	oldNeckC0 = neck.C0
	shoulder = torso:FindFirstChild("Right Shoulder")
	oldShoulderC0 = shoulder.C0
	
	-- Remember old mouse icon and update current
	oldIcon = mouse.Icon
	mouse.Icon = "http://www.roblox.com/asset/?id=170477320"
	
	-- Bind TouchMoved event if on mobile. Otherwise connect to renderstepped
	if userInputService.TouchEnabled then
		connection = userInputService.TouchMoved:connect(mobileFrame)
	else
		connection = render:connect(pcFrame)
	end
	
	-- Bind TouchStarted and TouchEnded. Used to determine if character should rotate
	-- during touch input
	userInputService.TouchStarted:connect(function(touch, processed)
		mobileShouldTrack = not processed
	end)	
	userInputService.TouchEnded:connect(function(touch, processed)
		mobileShouldTrack = false
	end)
	
	-- Fire server's equip event
	game.ReplicatedStorage.ROBLOX_PistolEquipEvent:FireServer()
	
	-- Bind event for when mouse is clicked to fire server's fire event
	mouse.Button1Down:connect(function()
		game.ReplicatedStorage.ROBLOX_PistolFireEvent:FireServer(mouse.Hit.p)
	end)
	
	-- Bind reload event to mobile button and r key
	contextActionService:BindActionToInputTypes("Reload", function() 
		game.ReplicatedStorage.ROBLOX_PistolReloadEvent:FireServer()		
	end, true, "")
	
	-- If game uses filtering enabled then need to update server while tool is
	-- held by character.
	if workspace.FilteringEnabled then
		while connection do
			wait()
			game.ReplicatedStorage.ROBLOX_PistolUpdateEvent:FireServer(neck.C0, shoulder.C0)
		end
	end
end
