Need help disable laser through wall

  1. What do you want to achieve?
    I want my laser, not through a wall, it looks like laser cheating, I need you guys to help me fix this bug, it’s making me crazy
  2. What is the issue?
    Here is video about that bug:
    I dont want it not go though wall like this, it looking very bad

    Not like this, please help me it
  3. What solutions have you tried so far?

I had trying to add more some script or deleted a bit script but it not working, here is what script have
SCRIPT 1

wait(0)
local Tool = script.Parent
Handle = Tool.Handle
function Playerget()
	N=Tool.Parent.Name
	F=Game.Players:findFirstChild(N)
	return F
end
function NewLazer(Pos1,Pos2)
	P=Instance.new("Part")
	P.Name = "Lazer" 
	P.BrickColor = BrickColor.new("Persimmon")--Control the color here
	P.Transparency= 0.7
	P.Anchored = true 
	P.CanCollide = false
	P.CanTouch = false
	P.CanQuery = false
	P.CastShadow = false
	P.Material = Enum.Material.Neon
	P.formFactor = 0
	P.Size = Vector3.new(0.005,0.005,(Pos1-Pos2).magnitude) 
	P.CFrame = CFrame.new((Pos1+Pos2)/2,Pos1)+Vector3.new(0,0,0)
	M=Instance.new("BlockMesh")
	M.Scale = Vector3.new(0.03,0.03,1)
	M.Parent=P
	P.Parent = Tool.Spam
	return P
end
function Point(mouse)
	X = string.sub(mouse.Hit.X,1,4)
	Y = string.sub(mouse.Hit.Y,1,4)
	Z = string.sub(mouse.Hit.Z,1,4)
	Compile = Vector3.new(X,Y,Z)
	Lazer = NewLazer(Compile,Handle.Position)
	while true do	
		X = string.sub(mouse.Hit.X,1,4)
		Y = string.sub(mouse.Hit.Y,1,4)
		Z = string.sub(mouse.Hit.Z,1,4)
		Compile = Vector3.new(X,Y,Z)
		Lazer.Size = Vector3.new(1,1,(Compile-Handle.Position).magnitude) 
		Lazer.CFrame = CFrame.new((Compile+Handle.Position)/2,Compile)+Vector3.new(0,0,0)
		wait(0)
	end
end
function EQuip(mouse) 
	if(mouse~=nil)then
		Point(mouse)
	end
end
Tool.Equipped:connect(EQuip)

SCRIPT 2

wait(0)
local Tool = script.Parent
function DeQuip()
	Spam=Tool.Spam:GetChildren()
	for i=1,#Spam do
		Spam[i]:Remove()
	end
end
Tool.Unequipped:connect(DeQuip)

To prevent the laser from going through the wall, you need to add a collision detection function to the script. Here’s how you can modify the NewLazer function to achieve this:

function NewLazer(Pos1, Pos2)
    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = {Tool.Parent}
    raycastParams.IgnoreWater = true
    local result = workspace:Raycast(Pos1, Pos2 - Pos1, raycastParams)
    if result then
        Pos2 = result.Position
    end
    local P = Instance.new("Part")
    P.Name = "Lazer" 
    P.BrickColor = BrickColor.new("Persimmon") --Control the color here
    P.Transparency = 0.7
    P.Anchored = true 
    P.CanCollide = false
    P.CanTouch = false
    P.CanQuery = false
    P.CastShadow = false
    P.Material = Enum.Material.Neon
    P.formFactor = 0
    P.Size = Vector3.new(0.005, 0.005, (Pos1 - Pos2).magnitude) 
    P.CFrame = CFrame.new((Pos1 + Pos2) / 2, Pos1) + Vector3.new(0, 0, 0)
    local M = Instance.new("BlockMesh")
    M.Scale = Vector3.new(0.03, 0.03, 1)
    M.Parent = P
    P.Parent = Tool.Spam
    return P
end

In this modified NewLazer function, a RaycastParams object is created with a filter that includes the parent of the tool (i.e., the player). The workspace:Raycast method is then used to check for collisions between the laser and other parts in the game. If a collision is detected, the Pos2 parameter is updated to the position of the collision.

This should prevent the laser from passing through walls

The UPDATED SCRIPT SHOULD LOOK LIKE THIS:

wait(0)
local Tool = script.Parent
Handle = Tool.Handle
function Playerget()
    N=Tool.Parent.Name
    F=Game.Players:findFirstChild(N)
    return F
end
function NewLazer(Pos1, Pos2)
    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = {Tool.Parent}
    raycastParams.IgnoreWater = true
    local result = workspace:Raycast(Pos1, Pos2 - Pos1, raycastParams)
    if result then
        Pos2 = result.Position
    end
    local P = Instance.new("Part")
    P.Name = "Lazer" 
    P.BrickColor = BrickColor.new("Persimmon") --Control the color here
    P.Transparency = 0.7
    P.Anchored = true 
    P.CanCollide = false
    P.CanTouch = false
    P.CanQuery = false
    P.CastShadow = false
    P.Material = Enum.Material.Neon
    P.formFactor = 0
    P.Size = Vector3.new(0.005, 0.005, (Pos1 - Pos2).magnitude) 
    P.CFrame = CFrame.new((Pos1 + Pos2) / 2, Pos1) + Vector3.new(0, 0, 0)
    local M = Instance.new("BlockMesh")
    M.Scale = Vector3.new(0.03, 0.03, 1)
    M.Parent = P
    P.Parent = Tool.Spam
    return P
end
function Point(mouse)
    X = string.sub(mouse.Hit.X,1,4)
    Y = string.sub(mouse.Hit.Y,1,4)
    Z = string.sub(mouse.Hit.Z,1,4)
    Compile = Vector3.new(X,Y,Z)
    Lazer = NewLazer(Compile,Handle.Position)
    while true do    
        X = string.sub(mouse.Hit.X,1,4)
        Y = string.sub(mouse.Hit.Y,1,4)
        Z = string.sub(mouse.Hit.Z,1,4)
        Compile = Vector3.new(X,Y,Z)
        Lazer.Size = Vector3.new(1,1,(Compile-Handle.Position).magnitude) 
        Lazer.CFrame = CFrame.new((Compile+Handle.Position)/2,Compile)+Vector3.new(0,0,0)
        wait(0)
    end
end
function EQuip(mouse) 
    if(mouse~=nil)then
        Point(mouse)
    end
end
Tool.Equipped:connect(EQuip)

you don’t need to change anything in your second script. It’s responsible for cleaning up the laser parts when the tool is unequipped, which is unrelated to the issue of the laser going through walls. The modifications to the NewLazer function in the first script should take care of the problem.

Spam it’s Model, not tool so it’s still through wall @DeIusively

Use raycasting and use this video to visualize the ray as a laser: