I am Trying to make a simple laser using raycasts.
I did some debugging and everything works fine except the “raycastResult” is nil, and there is a part in front of the laser for the raycast to hit.
Script:
function LaserBeam()
local rayOrigin = script.Parent.Position
local rayDirection = Vector3.new(0,0,-1000)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
print(tostring(raycastResult))
if raycastResult then
local hitPart = raycastResult.Instance
local Pos = raycastResult.Position
--if hitPart.Name == "Reflector" then
local dist = (script.Parent.Position - Pos).Magnitude
local Beam = Instance.new("Part")
Beam.Anchored = true
Beam. CanCollide = false
print("Test")
Beam.Size = Vector3.new(0.5, 0.5, dist)
Beam.CFrame = CFrame.new(script.Parent.Position, Pos) * CFrame.new(0, 0, -dist/2)
Beam.Parent = script.Parent.Parent
--end
end
end
script.Parent.ClickDetector.MouseClick:Connect(function()
LaserBeam()
end)