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


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)
