I am trying to have a raycast perform a function if a certain part is hit. The ray does cast to the object perfectly but does not seem to detect it (it returns nil)…
I have tried to first remove the whitelist, which did not help, and I also tried to use the position of the cast, which obviously did not work since that is dependent on it not being nil.
Here is my code (I apologize for the mess):
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local camera = workspace.CurrentCamera
local doors = workspace:WaitForChild(“Doors”)
local bypass_list = {char}
function search()
local function finddetector(Pose)
local Closest
local Distance = 500
for _,child in pairs(doors:GetChildren()) do
for i, v in pairs(child:GetChildren()) do
if v.Name == "CardScanner1" or v.Name == "CardScanner2" then
for i, p in pairs(v:GetChildren()) do
if p.Name == "Detector1" or p.Name == "Detector2" then
local newDistance = (p.Position- Pose.Position).magnitude
if newDistance < Distance then
print("got the detector chief")
print("the detector i got is "..p.Name)
Closest = p
Distance = newDistance
end
else
table.insert(bypass_list,p)
end
end
end
end
end
return Closest
end
local detector = finddetector(char.Head)
local camera = workspace.CurrentCamera
local worldPoint = Vector3.new(0, 10, 0)
local vector, onScreen = camera:WorldToScreenPoint(detector.Position)
if onScreen then
local ray = Ray.new(char.Head.Position,(char.Head.CFrame.Position - detector.CFrame.lookVector.unit * 300))
local part, position = workspace:FindPartOnRayWithIgnoreList(ray,bypass_list)
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("Bright red")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
beam.Name = "beam"
local distance = (char.Head.Position - detector.Position).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(char.Head.Position, detector.Position) * CFrame.new(0, 0, -distance / 2)
table.insert(bypass_list,beam)
game:GetService("Debris"):AddItem(beam, 0.1)
if part ~= nil and (position - detector.Position).magnitude < 3 then
print(detector.Name.." HEREEEEE :D")
local a = Instance.new("SelectionBox")
a.Parent = part
a.Adornee = part
a.Color3 = Color3.fromRGB(0,0,0)
elseif part ~= nil then
print(part.Name)
local a = tostring(position - detector.Position).magnitude
print(a)
print(bypass_list)
print(part.Parent)
else
print("nothing found was looking for "..detector.Name)
local a = tostring(position - detector.Position).magnitude
print(a)
end
end
end
while wait(.1) do
search()
end