I was working on a gun when I realized that you could shoot the gun and do damage even while the gun was unequipped.
Here’s the code:
script.Parent.Fire.OnServerEvent:Connect(function(player, mousePos)
if not debounce then
script.Parent.Settings.Ammo.Value = script.Parent.Settings.Ammo.Value - 1
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResults = workspace:Raycast(script.Parent.Handle.Position, (mousePos - script.Parent.Handle.Position)*300,raycastParams)
local ray = Ray.new(script.Parent.Handle.CFrame.p,(mousePos - script.Parent.Handle.CFrame.p).unit * 300)
local hit, position = workspace:FindPartOnRay(ray, script.Parent.Parent)
local distance = (position - script.Parent.Handle.CFrame.p).magnitude
local part = Instance.new("Part")
part.Anchored = true
part.CanCollide = false
part.Transparency = 0
part.BrickColor = BrickColor.new("Gold")
part.Size = Vector3.new(0.2, 0.2, distance)
part.CFrame = CFrame.new(position, script.Parent.Handle.BulletPosition.WorldCFrame.p) * CFrame.new(0, 0, -distance / 2)
part.Parent = workspace
game.Debris:AddItem(part, 0.1)
if raycastResults then
local hitPart = raycastResults.Instance
local model = hitPart:FindFirstAncestorOfClass("Model")
if model then
if model:FindFirstChild("Humanoid") then
model.Humanoid.Health -= 30
end
end
end
end
end)