local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")
local player = Players.LocalPlayer
local tool = script.Parent
local bodyAttach = tool:WaitForChild("BodyAttach")
local muzzle = bodyAttach:WaitForChild("Muzzle")
local fireRemote = ReplicatedStorage:WaitForChild("FireGun")
local replicateBulletRemote = ReplicatedStorage:WaitForChild("ReplicateBullet")
local Config = require(tool:WaitForChild("Config"))
local bulletTemplate = ReplicatedStorage:WaitForChild("Misc"):WaitForChild("Gunstuff"):WaitForChild("projectiles"):WaitForChild("Bullet")
local lastShotTime = 0
local function CreateBullet(origin, direction)
local bullet = bulletTemplate:Clone()
bullet.Position = origin
bullet.Anchored = false
bullet.CanCollide = false
bullet.CanQuery = false
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bodyVelocity.Velocity = direction * Config.GunSettings.BulletSpeed
bodyVelocity.Parent = bullet
bullet.Parent = workspace
Debris:AddItem(bullet, 5)
end
local function OnActivated()
if not tool.Enabled then return end
local currentTime = tick()
if currentTime - lastShotTime < Config.GunSettings.FireRate then return end
lastShotTime = currentTime
local mouse = player:GetMouse()
local mousePos = mouse.Hit.Position
local origin = muzzle.WorldPosition
local direction = (mousePos - origin).Unit
CreateBullet(origin, direction)
fireRemote:FireServer(mousePos)
end
replicateBulletRemote.OnClientEvent:Connect(function(shooter, origin, direction)
if shooter ~= player then
CreateBullet(origin, direction)
end
end)
tool.Activated:Connect(OnActivated)
local script