Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

RayCasting returning nil and I cannot seem to figure out why?

My raycasting argument returns nil even though the beam is finding the part and casting perfectly? I am wondering why it isn't detecting the part?


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

1 answer

So I asked on the devforum and.... my ray was backwards.... oops.

1 comment

1

LOL

This discussion is preserved as it was. New answers, comments, and votes are closed.