I have a rig in workspace, There is a part called sight welded to the Torso
Script #1 - located under the rig
ocal fire = script.Fire
fire = script.Parent.Humanoid.Animator:LoadAnimation(fire)
local D = game:GetService("Debris")
local Hit
local canShoot = false
local Sight = script.Parent.Sight
local SeeingTarget = script.SeeingTarget
local Debounce = false
local Ammo = 5000
local usedAmmo = 0
function Shoot()
local bullet = script.Bullet:Clone()
bullet.Parent = workspace
bullet.Transparency = 0
bullet.Position = script.Parent["M4A1 Model"].BarrelCover.Attachment.WorldPosition
bullet.Orientation = Vector3.new(math.random(1, 360), math.random(1, 360))
bullet.AssemblyLinearVelocity = Vector3.new(10, 10, 2)
bullet.AssemblyLinearVelocity = Vector3.new(10, -1000, 2)
D:AddItem(bullet, 5)
fire:Play()
wait(.1)
bullet.AssemblyLinearVelocity = Vector3.zero
task.wait(0.05)
local Part = script.Part:Clone()
game:GetService("Debris"):AddItem(Part, 10)
Part.Parent = workspace
Part.Transparency = 0
Part.Position = script.Parent["M4A1 Model"].BarrelCover.Attachment.WorldPosition
Part.Orientation = Vector3.new(0, 0, 0)
local Attachment = Instance.new("Attachment", Part) -- the attachment used in Linearvelocity
local LV = Instance.new("LinearVelocity", Attachment) -- creating the linear velocity
LV.MaxForce = math.huge -- no need to worry about this
LV.VectorVelocity = script.Parent.HumanoidRootPart.CFrame.lookVector * 1000 -- change 100 with how fast you want the projectile to go
LV.Attachment0 = Attachment-- setting the attachment
script.Part.Script:Clone().Parent = Part
usedAmmo += 1
task.wait(.2)
end
local RaycastResult = workspace:Raycast(script.Parent:WaitForChild("Head").Position, script.Parent:WaitForChild("Head").RayAttachment.WorldPosition - script.Parent:WaitForChild("Head").Position)
script.Parent.SeesTarget.Event:Connect(function(bool)
SeeingTarget.Value= bool
end)
SeeingTarget.Changed:Connect(function()
if Debounce == false then
Shoot()
end
Debounce = true
task.wait(0.1)
Debounce = false
end)
local BoundsCF = script.Parent.Sight.CFrame
local boundsParams = OverlapParams.new()
boundsParams.FilterType = Enum.RaycastFilterType.Exclude
boundsParams.FilterDescendantsInstances = {script.Parent}
Script#2 - located under sight
script.Parent.Touched:Connect(function(Part)
script.Parent.Parent.SeesTarget:Fire(true)
end)
script.Parent.TouchEnded:Connect(function()
script.Parent.Parent.SeesTarget:Fire(false)
end)
game:GetService("RunService").Heartbeat:Connect(function()
script.Parent.CFrame = script.Parent.CFrame * CFrame.Angles(0, math.rad(1), 0)
end)```