[ Solved ] Raycast ignore list. ray goes in different directions when passed through part

ray goes in different directions when passed through part.

Why and how do i fix this?

localscript

1. wait()
2. local player = game.Players.LocalPlayer
3. local Torso = player.Character.Torso
4. local mouse = player:GetMouse()
5.  
6. mouse.Button1Down:connect(function()
7. 	    print("Mouse pressed!")
8. 	
9. 	    local ray = Ray.new(Torso.CFrame.p, (mouse.Hit.p - Torso.CFrame.p).unit * 100)
10. 	local part, position = workspace:FindPartOnRayWithIgnoreList(ray, {game.Workspace.Part})
11. 	print(part)
12. 	local beam = Instance.new("Part", workspace)
13. 	beam.BrickColor = BrickColor.new("Bright red")
14. 	beam.FormFactor = "Custom"
15. 	beam.Material = "Neon"
16. 	beam.Transparency = 0.5
17. 	beam.Anchored = true
18. 	beam.Locked = true
19. 	beam.CanCollide = false
20. 	
21. 	local distance = (Torso.CFrame.p - position).magnitude
22. 	beam.Size = Vector3.new(0.3, 0.3, distance) -- beam.Size = Vector3.new(0.3, 0.3, distance)
23. 	beam.CFrame = CFrame.new(Torso.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
24. 	
25. 	game:GetService("Debris"):AddItem(beam, 0.1)
26. end)
local workspaceService = game:GetService("Workspace")
local playersService = game:GetService("Players")
local userInputService = game:GetService("UserInputService")

local localPlayer = playersService.LocalPlayer
local currentCamera = workspaceService.CurrentCamera

-- Instance<BasePart>?, Vector3, Vector3, EnumItem<Material> getMouseHit(table ignoreList)
local function getMouseHit(ignoreList)
    local mouseLocation = userInputService:GetMouseLocation()
    local viewportPointRay = currentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
    local extendedRay = Ray.new(viewportPointRay.Origin, viewportPointRay.Direction * 1000)
    
    return workspaceService:FindPartOnRayWithIgnoreList(extendedRay, ignoreList)
end

local mouse = localPlayer:GetMouse()
local Center = localPlayer.Character:WaitForChild("HumanoidRootPart")

mouse.Button1Down:connect(function()
	print("Mouse pressed!")

	local ignoreList = {localPlayer.Character,workspace.Part}
	local part, position = getMouseHit(ignoreList)
	print(part)
 	local beam = Instance.new("Part")
 	beam.BrickColor = BrickColor.new("Bright red")
 	beam.FormFactor = "Custom"
 	beam.Material = "Neon"
 	beam.Transparency = 0.5
 	beam.Anchored = true
 	beam.Locked = true
 	beam.CanCollide = false
	beam.Name = "Beam"
	beam.Parent = workspace
 	
 	local distance = (Center.CFrame.p - position).magnitude
 	beam.Size = Vector3.new(0.2, 0.2, distance)
 	beam.CFrame = CFrame.new(Center.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
	game:GetService("Debris"):AddItem(beam, 0.1)
end)

--Credit to CreditsMix for helping me search for the solution.
--Credit to dandystan for the getMouseHit function.

Credit to CreditsMix for helping me search for the solution.
Credit to dandystan for the getMouseHit function.

2 Likes