Raycast going through a part

Yo, I’m new to this raycast thingy, So I got this from a forum post a long time ago ( I can’t find it )

LocalScript: ( Not full Script )

local Players = game:GetService("Players")

local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local Mouse = Players.LocalPlayer:GetMouse()
local Character = Players.LocalPlayer.Character
local Camera = game.Workspace.CurrentCamera

local function RaycastMouse()
	local Blacklist = {}
	
	-- ignore self
	for i, Baseparts in pairs(Character:GetDescendants()) do
		if Baseparts:IsA("BasePart") then
			table.insert(Blacklist, Baseparts)
		end
	end
	
	-- ignore parts with no collisions
	for i, Baseparts in pairs(game.Workspace:GetDescendants()) do
		if Baseparts:IsA("BasePart") then
			if Baseparts.CanCollide == false then
				table.insert(Blacklist, Baseparts)
			end
		end
	end
	
	-- Ignore teammates
	for i, Player in pairs(Players:GetPlayers()) do
		if Player.Team == Players.LocalPlayer.Team and Player.Character then
			for i, Baseparts in pairs(Player.Character:GetDescendants()) do
				if Baseparts:IsA("BasePart") then
					table.insert(Blacklist, Baseparts)
				end
			end
		end
	end
	
	local RaycastParameters = RaycastParams.new()
	RaycastParameters.FilterDescendantsInstances = Blacklist
	RaycastParameters.FilterType = Enum.RaycastFilterType.Blacklist
	
	local UnitRay = Camera:ScreenPointToRay(Mouse.X, Mouse.Y)
	
	return workspace:Raycast(UnitRay.Origin, UnitRay.Direction * 100, RaycastParameters)
end

It works, but the raycast is going right through walls

so I changed UnitRay.Origin to the ShootPos on the handle

return workspace:Raycast(Handle.ShootPos.Position, UnitRay.Direction * 100, RaycastParameters)

It works but the bullets go in the wrong directions

so I searched through the DevForum and got nothing



help

what is t

I mean

try Hit.Position

return workspace:Raycast(UnitRay.Origin, Mouse.Hit.Position, RaycastParameters)

the function returns “nil”

I’ve also tried that before and now the bullet doesn’t exist

Can you print out mouse.Hit?

4.109394073486328, -2.0000009536743164, -6.366777420043945

output:

  -85.43988037109375, -2, 36.55193328857422  -  Client
  -85.82645416259766, -2, 37.48530578613281  -  Client
  -84.18201446533203, -2, 38.98938751220703  -  Client
  -90.89468383789062, -2.0000009536743164, 55.72196960449219  -  Client
  -91.74897766113281, -2, 56.676300048828125  -  Client
  -90.54084777832031, -2.000001907348633, 54.984703063964844  -  Client
  -88.38114929199219, -2.000002861022949, 60.625732421875  -  Client
  -83.7139892578125, -1.9999990463256836, 69.02626037597656  -  Client
  -87.20911407470703, -2, 102.49444580078125  -  Client
  -109.79354858398438, -1.9999990463256836, 115.37516784667969 (x3)  -  Client
  -8.320732116699219, -2, 41.399871826171875  -  Client
  -5.2030487060546875, -1.9999990463256836, 39.66057586669922  -  Client
  18.349411010742188, -1.9999980926513672, 25.89666175842285  -  Client
  30.300819396972656, -1.9999980926513672, 26.23991584777832  -  Client
  26.46623992919922, -2, 21.971389770507812  -  Client
  -4.6605072021484375, -1.9999990463256836, 16.35576820373535  -  Client
  -4.216041564941406, -2.0000009536743164, 19.482023239135742  -  Client
  -1.9260330200195312, -1.9999990463256836, 21.365013122558594  -  Client
  -3.4105300903320312, -2.000001907348633, 25.522045135498047  -  Client
  30.08614730834961, 3.5479602813720703, 12.94662857055664  -  Client
  30.44931411743164, 3.790614366531372, 11.972698211669922  -  Client
  30.800647735595703, 3.8476409912109375, 11.861948013305664  -  Client
  31.036128997802734, 3.904827356338501, 12.536813735961914  -  Client
  28.591304779052734, 2.9811809062957764, 12.903632164001465  -  Client
  144.18043518066406, 7.716269493103027, 47.686241149902344  -  Client
  37.4671516418457, 6.668037414550781, 11.791406631469727  -  Client
  37.4671516418457, 6.527793884277344, 13.09942626953125  -  Client
  37.4671516418457, 6.898398399353027, 12.57309341430664  -  Client
  37.4671516418457, 6.482341766357422, 14.051887512207031  -  Client
  37.4671516418457, 6.168776988983154, 13.993938446044922  -  Client
  118.84764099121094, 8.906291007995605, 59.95903778076172  -  Client
  8640.888671875, 431.49481201171875, 4948.37890625  -  Client
  9073.5048828125, 327.16973876953125, 4123.71240234375  -  Client
  8659.6044921875, 398.49755859375, 4927.10302734375  -  Client
  4.109394073486328, -2.0000009536743164, -6.366777420043945  -  Client

Replace the 1st argy with Handle.ShootPos.Position

return workspace:Raycast(Handle.ShootPos.Position, UnitRay.Direction * 500, RaycastParameters)
1 Like

ong it works. It doesn’t go through a part now

but the raycast goes a bit down than it supposed to be, it keeps going on the floor instead of the mouse position

full script ( I mean function ) :

local Players = game:GetService("Players")

local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local Mouse = Players.LocalPlayer:GetMouse()
local Character = Players.LocalPlayer.Character
local Camera = game.Workspace.CurrentCamera

local function RaycastMouse()
	local Blacklist = {}

	-- ignore self
	for i, Baseparts in pairs(Character:GetDescendants()) do
		if Baseparts:IsA("BasePart") then
			table.insert(Blacklist, Baseparts)
		end
	end

	-- ignore parts with no collisions
	for i, Baseparts in pairs(game.Workspace:GetDescendants()) do
		if Baseparts:IsA("BasePart") then
			if Baseparts.CanCollide == false then
				table.insert(Blacklist, Baseparts)
			end
		end
	end

	-- Ignore teammates
	for i, Player in pairs(Players:GetPlayers()) do
		if Player.Team == Players.LocalPlayer.Team and Player.Character then
			for i, Baseparts in pairs(Player.Character:GetDescendants()) do
				if Baseparts:IsA("BasePart") then
					table.insert(Blacklist, Baseparts)
				end
			end
		end
	end

	local RaycastParameters = RaycastParams.new()
	RaycastParameters.FilterDescendantsInstances = Blacklist
	RaycastParameters.FilterType = Enum.RaycastFilterType.Blacklist

	local UnitRay = Camera:ScreenPointToRay(Mouse.X, Mouse.Y)

	return workspace:Raycast(Handle.ShootPos.Position, UnitRay.Direction * 500, RaycastParameters)
end

and replace the 2nd argy with Mouse.UnitRay.Direction

then remove the UnitRay variable because I think it’s useless

return workspace:Raycast(Handle.ShootPos.Position, Mouse.UnitRay.Direction * 500, RaycastParameters)
local Players = game:GetService("Players")

local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local Mouse = Players.LocalPlayer:GetMouse()
local Character = Players.LocalPlayer.Character
local Camera = game.Workspace.CurrentCamera

local function RaycastMouse()
	local Blacklist = {}

	-- ignore self
	for i, Baseparts in pairs(Character:GetDescendants()) do
		if Baseparts:IsA("BasePart") then
			table.insert(Blacklist, Baseparts)
		end
	end

	-- ignore parts with no collisions
	for i, Baseparts in pairs(game.Workspace:GetDescendants()) do
		if Baseparts:IsA("BasePart") then
			if Baseparts.CanCollide == false then
				table.insert(Blacklist, Baseparts)
			end
		end
	end

	-- Ignore teammates
	for i, Player in pairs(Players:GetPlayers()) do
		if Player.Team == Players.LocalPlayer.Team and Player.Character then
			for i, Baseparts in pairs(Player.Character:GetDescendants()) do
				if Baseparts:IsA("BasePart") then
					table.insert(Blacklist, Baseparts)
				end
			end
		end
	end

	local RaycastParameters = RaycastParams.new()
	RaycastParameters.FilterDescendantsInstances = Blacklist
	RaycastParameters.FilterType = Enum.RaycastFilterType.Blacklist

	local UnitRay = Camera:ScreenPointToRay(Mouse.X, Mouse.Y)

	return workspace:Raycast(Handle.ShootPos.Position, Mouse.UnitRay.Direction * 500, RaycastParameters)
end

yes

its fixed

after 4 weeks of research

thank you