Custom Interaction System - Raycasting Issue

So I’ve been working on this custom interaction system, it’s working fine but I’ve got an issue when I’ve tried to implement a feature that detects if the player can see the interactable (the interactable instance), I’m confused why this isn’t working because I used to do these kind of stuffs alot, here’s my current code:

function RaycastInter(interactable)
	print("RAYCAST")
	local ignoreList = {}
	
	for i, v in pairs(Player.Character:GetDescendants()) do
		if v:IsA("BasePart") then
			table.insert(ignoreList, v)
		end
	end
	
	for i, v in pairs(workspace:GetDescendants()) do
		if v:IsA("BasePart") and v.Transparency == 1 or v.Name == "Laser" then
			table.insert(ignoreList, v)
		end
	end
	
	for i, v in pairs(interactable:GetDescendants()) do
		if v:IsA("BasePart") then
			table.insert(ignoreList, v)
		end
	end
	
	local RP = RaycastParams.new()
	RP.FilterDescendantsInstances = ignoreList
	RP.FilterType = Enum.RaycastFilterType.Blacklist
	
	local origin = Char.HumanoidRootPart.Position
	local target = interactable
	local direction = (target.Position - origin).unit * 10000
	
	local results = workspace:Raycast(origin, direction, RP)
	
	local laserPart = Instance.new("Part")
	laserPart.Anchored = true 
	laserPart.Name = "Laser"
	laserPart.BrickColor = BrickColor.new("Really red")
	laserPart.CanCollide = false
	laserPart.Size = Vector3.new(0.2, 0.2, (origin - target.Position).Magnitude)
	laserPart.CFrame = CFrame.new(origin, target.Position) * CFrame.new(0, 0, -(origin - target.Position).Magnitude / 2)
	laserPart.Parent = workspace
	
	print(results)
	
	if not results then
		return true
	end
end

function Trigger(interactable)
	for i, v in pairs(interactable:GetChildren()) do
		if v:IsA("RemoteEvent") and v:FindFirstChild("IsInteractEvent") then
			if v:FindFirstChild("IsInteractEvent").Value == true then
				local dist = (interactable.Position - Char.HumanoidRootPart.Position).Magnitude
				if RaycastInter(interactable) then
					if dist < InteractionStats.Range then
						v:FireServer()
						PlayAnim(currentUI, "Interact")
					end
				end
			end
		end
	end
end

Here’s the video:

(The laser part is used just to visualize raycast)

1 Like

Bumping, still haven’t fixed the issue

It seems to work perfectly in the video. What’s the issue again?

It’s supposed to interact the door (open the door), you can clearly see that the door isn’t opening at all despite being exposed and not blocked by anyhting

1 Like

Ah, I see.

Place a breakpoint in your RaycastInter function, then step through your code and watch the relevant variables to make sure it actually does what you think it should do. For example, use it to check which part is obscuring line of sight. Or maybe it’s not even because a part is blocking LoS, could be something else.

As an aside you should probably put the distance check before the raycast check, because the vast majority of prompts is going to out of range at any given time, and comparing two numbers (distance less than range?) is muuuuch faster than raycasting, especially long raycasts which is exactly what you’ll get all the time if you first check for line of sight. It has the added benefit of only calling into the function you’re trying to debug if a prompt is in range, so you can actually walk up to the prompt without having the script constantly reach the breakpoint every time Trigger is called. If that’s once per frame, the game would be unplayable because it’d pause every frame.

1 Like

Alright, thanks, lemme try it.

1 Like

I found another solution, but since you’re the only one who replied, I’ll give you the solution mark. Thanks!

1 Like