local G=FastCast.new()local l=RaycastParams.new()l.FilterType=Enum.RaycastFilterType.Blacklist l.IgnoreWater=true local E=PartCache.new(bulletTemplate,100,bulletsFolder)local Y=FastCast.newBehavior()Y.RaycastParams=l Y.Acceleration=Vector3.new(0,-workspace.Gravity,0)Y.AutoIgnoreContainer=false Y.CosmeticBulletContainer=bulletsFolder Y.CosmeticBulletProvider=E local B=30 local p=false local function J()if p then return end p=true wait(5)B=30 p=false end local function t()l.FilterDescendantsInstances={tool.Parent,bulletsFolder}end local function A(G,l,E,Y,B,p)if p then local G=p.Size.Z/2 local B=CFrame.new(0,0,-(Y-G))p.CFrame=(CFrame.lookAt(l,l+E)):ToWorldSpace(B)end end local function K(G,l,Y,B)local p=l.Instance local J=p:FindFirstAncestorWhichIsA("\077\111\100\101\108")if J and J:FindFirstChild("\072\117\109\097\110\111\105\100")then J.Humanoid:TakeDamage(50)end delay(2,function()E:ReturnPart(B)end)end local function Z(l,E)if tool.Parent:IsA("\066\097\099\107\112\097\099\107")then return end if B>0 and not p then local l=firePoint.WorldPosition local p=(E-l).Unit G:Fire(l,p,1000,Y)B-=1 else J()end end fireEvent.OnServerEvent:Connect(Z)reloadEvent.OnServerEvent:Connect(J)tool.Equipped:Connect(t)G.LengthChanged:Connect(A)
--FastCast.VisualizeCasts = true

local caster = FastCast.new()

local castParams = RaycastParams.new()
castParams.FilterType = Enum.RaycastFilterType.Blacklist
castParams.IgnoreWater = true

local bulletCache = PartCache.new(bulletTemplate, 100, bulletsFolder)

local castBehavior = FastCast.newBehavior()
castBehavior.RaycastParams = castParams
castBehavior.Acceleration = Vector3.new(0, -workspace.Gravity, 0)
castBehavior.AutoIgnoreContainer = false
castBehavior.CosmeticBulletContainer = bulletsFolder
castBehavior.CosmeticBulletProvider = bulletCache

local ammoLeft = 30
local reloading = false

local function reload()
	if reloading then
		return
	end
	reloading = true
	wait(5)
	ammoLeft = 30
	reloading = false
end

local function onEquipped()
	castParams.FilterDescendantsInstances = { tool.Parent, bulletsFolder }
end

local function onLengthChanged(cast, lastPoint, direction, length, velocity, bullet)
	if bullet then
		local bulletLength = bullet.Size.Z / 2
		local offset = CFrame.new(0, 0, -(length - bulletLength))
		bullet.CFrame = CFrame.lookAt(lastPoint, lastPoint + direction):ToWorldSpace(offset)
	end
end

local function onRayHit(cast, result, velocity, bullet)
	local hit = result.Instance

	local character = hit:FindFirstAncestorWhichIsA("Model")
	if character and character:FindFirstChild("Humanoid") then
		character.Humanoid:TakeDamage(50)
	end

	delay(2, function()
		bulletCache:ReturnPart(bullet)
	end)
end

local function fire(player, mousePosition)
	if tool.Parent:IsA("Backpack") then
		return
	end

	if ammoLeft > 0 and not reloading then
		local origin = firePoint.WorldPosition
		local direction = (mousePosition - origin).Unit

		caster:Fire(origin, direction, 1000, castBehavior)
		ammoLeft -= 1
	else
		reload()
	end
end

fireEvent.OnServerEvent:Connect(fire)
reloadEvent.OnServerEvent:Connect(reload)
tool.Equipped:Connect(onEquipped)

caster.LengthChanged:Connect(onLengthChanged)
