I’ve made a laser gun that when fired, creates a beam to the mouse position. For whatever reason, when you shoot behind your character, the beam doesn’t destroy itself. I can’t figure out why, as there aren’t any errors being outputted.
Below is the function for casting the ray and all that.
function onActivated(plr, mouse)
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = ignoreList
local origin = Instance.new("Part")
origin.Size = Vector3.new(0.5,0.5,0.5)
origin.Transparency = 1
origin.Anchored = true
origin.CanCollide = false
origin.Position = mouse
origin.Parent = workspace
local a0, a1 = Instance.new("Attachment"), Instance.new("Attachment")
a0.Parent = origin
a1.Parent = hole
local trail = Instance.new("Beam")
trail.Width1 = 0.2
trail.Width0 = 0.2
trail.Segments = 0
trail.FaceCamera = true
trail.Attachment0 = a0
trail.Attachment1 = a1
trail.Parent = origin
hole.laserLight.Enabled = true
hole.laserSound.Pitch = math.random(90, 100)/100
hole.laserSound:Play()
local results = workspace:Raycast(hole.Position, (mouse - hole.Position).Unit * 999, params)
if results then
local hit = results.Instance
if hit then
if hit:IsDescendantOf(tool.Parent) then return end
local human = hit.Parent:FindFirstChild("Humanoid")
if human and human.Health > 0 then
if debounce then
debounce = false
tagHuman(human)
hit.Parent.Humanoid:TakeDamage(8)
wait(0.05)
debounce = true
end
end
end
end
wait(0.25)
hole.laserLight.Enabled = false
origin:Destroy()
a1:Destroy()
end
Please let me know if you have any idea to why this is happening.