function updateWeaponHud(u,c)clipUI.Text=u ammoUI.Text=c ammoUI.AmmoEffect.Text=c if c>ammoLeft then spawn(ammoEffect)end if u==0 then clipUI.TextColor3=Color3.new(.89019607843137,.11372549019608,.12549019607843)else clipUI.TextColor3=Color3.new(.78039215686275,.78039215686275,.78039215686275)end if c==0 then ammoUI.TextColor3=Color3.new(.89019607843137,.11372549019608,.12549019607843)else ammoUI.TextColor3=Color3.new(.78039215686275,.78039215686275,.78039215686275)end currentAmmo=u ammoLeft=c end if configs.infiniteAmmo==true then updateWeaponHud(currentAmmo,clipSize)else updateWeaponHud(currentAmmo,ammoLeft)end function equip()local u=player.Character if player.Character.Humanoid.RigType==Enum.HumanoidRigType.R6 then neck=player.Character.Torso.Neck oldNeckC0=neck.C0 shoulder=player.Character.Torso:FindFirstChild("\082\105\103\104\116 \083\104\111\117\108\100\101\114")oldShoulderC0=shoulder.C0 else neck=player.Character.Head.Neck oldNeckC0=neck.C0 shoulder=player.Character.RightUpperArm:FindFirstChild("\082\105\103\104\116\083\104\111\117\108\100\101\114")oldShoulderC0=shoulder.C0 end hud.AmmoHud:TweenPosition(UDim2.new(.98,0,.95,0),"\073\110\079\117\116","\081\117\097\100",.3,true)runService:BindToRenderStep("\083\105\103\104\116\115",Enum.RenderPriority.Last.Value,function()hud.Crosshair.Position=UDim2.new(0,mouse.X,0,mouse.Y)end)userInputService.MouseIconEnabled=false hud.Crosshair.Visible=true if userInputService.TouchEnabled then connection=userInputService.TouchMoved:Connect(mobileFrame)else connection=render:Connect(pcFrame)end userInputService.TouchStarted:Connect(function(u,c)mobileShouldTrack=not c end)userInputService.TouchEnded:Connect(function(u,c)mobileShouldTrack=false end);(game.ReplicatedStorage:WaitForChild("\082\079\066\076\079\088\095\080\105\115\116\111\108\069\113\117\105\112\069\118\101\110\116")):FireServer()mouse.Button1Down:Connect(function()(game.ReplicatedStorage:WaitForChild("\082\079\066\076\079\088\095\080\105\115\116\111\108\070\105\114\101\069\118\101\110\116")):FireServer(mouse.Hit.p)updateWeaponHud(currentAmmo,ammoLeft)end)contextActionService:BindActionToInputTypes("\082\101\108\111\097\100",function()(game.ReplicatedStorage:WaitForChild("\082\079\066\076\079\088\095\080\105\115\116\111\108\082\101\108\111\097\100\069\118\101\110\116")):FireServer()end,true,"\114")if workspace.FilteringEnabled then while connection do wait();(game.ReplicatedStorage:WaitForChild("\082\079\066\076\079\088\095\080\105\115\116\111\108\085\112\100\097\116\101\069\118\101\110\116")):FireServer(neck.C0,shoulder.C0)end end end local function u()if connection then connection:disconnect()end contextActionService:UnbindAction("\082\101\108\111\097\100");(game.ReplicatedStorage:WaitForChild("\082\079\066\076\079\088\095\080\105\115\116\111\108\085\110\101\113\117\105\112\069\118\101\110\116")):FireServer()hud.AmmoHud:TweenPosition(UDim2.new(1.5,0,.95,0),"\073\110\079\117\116","\081\117\097\100",.3,true)runService:UnbindFromRenderStep("\083\105\103\104\116\115")userInputService.MouseIconEnabled=true hud.Crosshair.Visible=false neck.C0=oldNeckC0 shoulder.C0=oldShoulderC0 end function hitmarkerUpdate()hud.Crosshair.Hitmarker.Visible=true wait(.1)hud.Crosshair.Hitmarker.Visible=false end

function updateWeaponHud(mag, ammo)
	clipUI.Text = mag
	ammoUI.Text = ammo
	ammoUI.AmmoEffect.Text = ammo
	if ammo > ammoLeft then
		spawn(ammoEffect)
	end
	if mag == 0 then
		clipUI.TextColor3 =  Color3.new(227/255, 29/255, 32/255)
	else
		clipUI.TextColor3 = Color3.new(199/255, 199/255, 199/255)
	end
	if ammo == 0 then
		ammoUI.TextColor3 =  Color3.new(227/255, 29/255, 32/255)
	else
		ammoUI.TextColor3 = Color3.new(199/255, 199/255, 199/255)
	end
	currentAmmo = mag
	ammoLeft = ammo
end

if configs.infiniteAmmo == true then
	updateWeaponHud(currentAmmo, clipSize)
else
	updateWeaponHud(currentAmmo, ammoLeft)
end

function equip()
	local char = player.Character

	-- Setup joint variables
	if player.Character.Humanoid.RigType == Enum.HumanoidRigType.R6 then
		neck = player.Character.Torso.Neck
		oldNeckC0 = neck.C0
		shoulder = player.Character.Torso:FindFirstChild("Right Shoulder")
		oldShoulderC0 = shoulder.C0
	else
		neck = player.Character.Head.Neck
		oldNeckC0 = neck.C0
		shoulder = player.Character.RightUpperArm:FindFirstChild("RightShoulder")
		oldShoulderC0 = shoulder.C0
	end

	-- Remember old mouse icon and update current
	hud.AmmoHud:TweenPosition(UDim2.new(0.98, 0, 0.95, 0), "InOut", "Quad", 0.3, true)
	runService:BindToRenderStep("Sights", Enum.RenderPriority.Last.Value, function()
		hud.Crosshair.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
	end)
	userInputService.MouseIconEnabled = false
	hud.Crosshair.Visible = true

	-- 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:WaitForChild("ROBLOX_PistolEquipEvent"):FireServer()

	-- Bind event for when mouse is clicked to fire server's fire event
	mouse.Button1Down:Connect(function()
		game.ReplicatedStorage:WaitForChild("ROBLOX_PistolFireEvent"):FireServer(mouse.Hit.p)
		updateWeaponHud(currentAmmo, ammoLeft)
	end)

	-- Bind reload event to mobile button and r key
	contextActionService:BindActionToInputTypes("Reload", function()
		game.ReplicatedStorage:WaitForChild("ROBLOX_PistolReloadEvent"):FireServer()
	end, true, "r")

	-- 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:WaitForChild("ROBLOX_PistolUpdateEvent"):FireServer(neck.C0, shoulder.C0)
		end
	end
end

local function unequip()
	if connection then connection:disconnect() end
	contextActionService:UnbindAction("Reload")
	game.ReplicatedStorage:WaitForChild("ROBLOX_PistolUnequipEvent"):FireServer()
	hud.AmmoHud:TweenPosition(UDim2.new(1.5, 0, 0.95, 0), "InOut", "Quad", 0.3, true)
	runService:UnbindFromRenderStep("Sights")
	userInputService.MouseIconEnabled = true
	hud.Crosshair.Visible = false
	neck.C0 = oldNeckC0
	shoulder.C0 = oldShoulderC0
end

function hitmarkerUpdate()
	hud.Crosshair.Hitmarker.Visible = true
	wait(0.1)
	hud.Crosshair.Hitmarker.Visible = false
end
