The laser of my laser gun goes through stuff

Hello, I’m trying to make a spleef game with laser guns. And I want the ray to be visible(which i did, but not as expected). So the ray doesn’t go through walls but the laser does. There are 2 scripts, 1 local script and 1 server script.
Here is the local script

local SpleefGun = script.Parent
local ShootEvent = SpleefGun:WaitForChild("ShootEvent")
local Mouse = game.Players.LocalPlayer:GetMouse()
local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local HRP = Character:WaitForChild("HumanoidRootPart")
SpleefGun.Activated:Connect(function()

	if Mouse.Target ~= nil  and (Mouse.Hit.p - HRP.Position).Magnitude <= SpleefGun.Range.Value then
	ShootEvent:FireServer(Mouse.Hit.p, Mouse.Hit.LookVector, Mouse.Target)
	end
end)

server script:

local SpleefGun = script.Parent
local Range = SpleefGun.Range.Value
local ShootEvent = SpleefGun.ShootEvent
local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(0.31, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)

local Goals = {
  Transparency = 1
}
ShootEvent.OnServerEvent:Connect(function(Player, MousePos, MouseVector, Target)
	
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local HRP = Character:WaitForChild("HumanoidRootPart")
	if Target ~= nil  and (MousePos - HRP.Position).Magnitude <= Range then
		local BlackListedItems = {Character:GetDescendants()}
		local Origin = SpleefGun.Handle.CFrame.Position
		local Direction = (MousePos - SpleefGun.Handle.Position) * 1000
		local RayCastWhiteList = RaycastParams.new()
		RayCastWhiteList.FilterType = Enum.RaycastFilterType.Blacklist
		RayCastWhiteList.FilterDescendantsInstances = BlackListedItems

		local Shoot = workspace:Raycast(Origin, (MousePos - SpleefGun.Handle.Position) * Range, RayCastWhiteList)
		if Shoot then
			local Block = Shoot.Instance
			local LaserPart = Instance.new("Part", workspace)
			local Distance = (MousePos - Origin).Magnitude
	print(Block)
			LaserPart.Anchored = true
			LaserPart.BrickColor = BrickColor.new("Bright red")
			LaserPart.Material = Enum.Material.Neon
			LaserPart.CanCollide = false
			LaserPart.CFrame = CFrame.new(Origin, MousePos) * CFrame.new(0,0, -Distance / 2) 
			LaserPart.Size = Vector3.new(0.2, 0.2, Distance)
			local DestroyLaser = TweenService:Create(LaserPart, Info, Goals)
			DestroyLaser:Play()
			DestroyLaser.Completed:Connect(function()
				LaserPart:Destroy()
			end)
			if Block.Name == "Block" then
				local DestroyTarget = TweenService:Create(Block, Info, Goals)
				DestroyTarget:Play()
				DestroyTarget.Completed:Connect(function()
					Block:Destroy()
					end)
			end
	
			
			
		
		end
	end	
end)

Any help would be appreciated! Thanks in advance!

1 Like

I fixed the laser by making the laser go to the rays position. Here’s the new server script if u want to see it.

local SpleefGun = script.Parent
local Range = SpleefGun.Range.Value
local ShootEvent = SpleefGun.ShootEvent
local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(0.31, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)

local Goals = {
  Transparency = 1
}
ShootEvent.OnServerEvent:Connect(function(Player, MousePos, MouseVector, Target)
	
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local HRP = Character:WaitForChild("HumanoidRootPart")
	if Target ~= nil  and (MousePos - HRP.Position).Magnitude <= Range then
		local BlackListedItems = {Character:GetDescendants()}
		local Origin = SpleefGun.Handle.CFrame.Position
		local Direction = (MousePos - SpleefGun.Handle.Position) * 1000
		local RayCastWhiteList = RaycastParams.new()
		RayCastWhiteList.FilterType = Enum.RaycastFilterType.Blacklist
		RayCastWhiteList.FilterDescendantsInstances = BlackListedItems

		local Shoot = workspace:Raycast(Origin, (MousePos - SpleefGun.Handle.Position) * Range, RayCastWhiteList)
		if Shoot then
			local Block = Shoot.Instance
			local LaserPart = Instance.new("Part", workspace)
			local Distance = (Shoot.Position - Origin).Magnitude
	print(Block)
			LaserPart.Anchored = true
			LaserPart.BrickColor = BrickColor.new("Bright red")
			LaserPart.Material = Enum.Material.Neon
			LaserPart.CanCollide = false
			LaserPart.CFrame = CFrame.new(Origin, Shoot.Position) * CFrame.new(0,0, -Distance / 2) 
			LaserPart.Size = Vector3.new(0.2, 0.2, Distance)
			local DestroyLaser = TweenService:Create(LaserPart, Info, Goals)
			DestroyLaser:Play()
			DestroyLaser.Completed:Connect(function()
				LaserPart:Destroy()
			end)
			if Block.Name == "Block" then
				local DestroyTarget = TweenService:Create(Block, Info, Goals)
				DestroyTarget:Play()
				DestroyTarget.Completed:Connect(function()
					Block:Destroy()
					end)
			end
	
			
			
		
		end
	end	
end)