Laser gun beam problem

7875c1d75845b2def1c820e079d22942

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.

1 Like

Heyo! The problem is you are checking if result is a descendant of the character then you terminate the operation not allowing it to go down to :Destroy(). Instead of having to do that detection just use Raycast Perms Blacklist. I hope this works! RaycastParams | Roblox Creator Documentation

1 Like

The problem is you are checking if result is a descendant of the character then you terminate the operation not allowing it to go down to :Destroy(). Instead of having to do that detection just use Raycast Perms Blacklist.

I thought so. I already have a blacklist, but I did that because I don’t want the laser beams to be damage the player. Though, I see now that I don’t have to do that.

local ignoreList = {tool, tool.Parent}

I should already have the player’s character in the ignoreList table, though there’s a possibility that the tool.Parent that it’s reading off of is the player’s Backpack object. Any idea on how I could prevent this?

I’m pretty sure this problem won’t happen as long as this function is called whilst the tool is equipped.

Well…

fa24707c0bb28d69deec2433bbb502f6

What are you defining tool as? (Ignore check second reply)

Actually scratch what I just said, move where your defining ignore list into the activated function.

I figured it out, thank you for helping!

1 Like
if hit:IsDescendantOf(tool.Parent) then
	hole.laserLight.Enabled = false
	origin:Destroy()
	a1:Destroy()
end