I was making a weapon using the ‘Fire Cast’ module, but how can I make the bullet remove itself once it reaches its maximum range if it didn’t touch any players?
Script:
local Replicated = game:GetService("ReplicatedStorage")
local FastCast = require(Replicated:FindFirstChild("Main").FastCast)
local Hitbox = require(Replicated.Main:FindFirstChild("HitBox"))
FastCast.VisualizeCasts = false
local Tool = script.Parent
local Ammo = 30
local MaxAmmo = 30
local Shooting = false
local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Blacklist
Tool.Equipped:Connect(function()
Params.FilterDescendantsInstances = {Tool.Parent}
end)
--[[Fire, Shot]]
function Fast(Direction)
if Ammo == 0 or Ammo < 1 then
Shooting = false
return
end
if Shooting == false then
Shooting = true
wait(.1)
Ammo -= 1
local Bullet = game:GetService("ServerStorage").Bullets:FindFirstChild("MainBullet"):Clone()
Bullet.Parent = workspace
local Origin = Tool.Handle.Attachment.WorldPosition
local Look = (Direction - Origin).Unit
local Cast = FastCast.new()
local Behavior = Cast.newBehavior()
Behavior.RaycastParams = Params
Behavior.MaxDistance = 100
Cast.RayHit:Connect(function(Ray, Object)
local Hit = Object.Instance
local Character = Hit:FindFirstAncestorOfClass("Model") or Hit.Parent
local Humanoid = Character:FindFirstChild("Humanoid")
if Character and Humanoid then
Humanoid:TakeDamage(math.random(4, 7))
wait(.1)
Bullet:Destroy()
else
wait(3)
Bullet:Destroy()
end
end)
Cast.LengthChanged:Connect(function(Cast, LastPoint, Dir, Length)
local Blengh = Bullet.Size.Z/2
local OffSet = CFrame.new(0, 0, -(Length-Blengh))
Bullet.CFrame = CFrame.lookAt(LastPoint, LastPoint+Dir):ToWorldSpace(OffSet)
end)
Cast:Fire(Origin, Look, 150, Behavior)
end
wait(1)
Shooting = false
end
Tool:FindFirstChild("Shoot").OnServerEvent:Connect(function(Player, Position)
Fast(Position.Position)
end)