Raycast not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    When part is in front of a player, its transparency is .75, and when its not in front of the player anymore, its transparency goes back to 0. + I need to make the raycast detecting multiple parts.
  2. What is the issue?
    Raycast not working at all. No result and no errors in output.

here is the code:

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local function raycast(p0, p1, ignoreParts) 
	local ignoreTable = ignoreParts

	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.IgnoreWater = true
	params.FilterDescendantsInstances = {ignoreTable} 

	local hitItems = {}
	local finished = false

	repeat	
		params.FilterType = Enum.RaycastFilterType.Exclude
		params.IgnoreWater = true
		params.FilterDescendantsInstances = {ignoreTable}
		local result = workspace:Raycast(p0, p1 - p0, params)
		if result then
			if result.Instance then
				table.insert(hitItems, result.Instance)
				table.insert(ignoreTable, result.Instance)
				print("Instance Found!")
			else				
				print("No more instance")
				finished = true
			end
			task.wait()		
		end
		task.wait()
	until finished == true
	return hitItems
end

local p0 = workspace.CurrentCamera.CFrame.p
local p1 = player.Character:FindFirstChild("HumanoidRootPart").Position
local ignore = {char:GetDescendants()}

while true do
	task.wait()
	local items = raycast(p0, p1, ignore)
end

I really dont know what to do

Looks like one problem may be that you’re defining p0 and p1 outside of the loop; they will always stay the same, even when the character moves. Try putting those into the loop.

Above is another: ignoreTable is already a table, but you’re wrapping it in more brackets. You’re essentially saying {{char:GetDescendants()}} instead of {char:GetDescendants()}. RaycastParams takes an array, not an array of arrays.

On a side note, why have the ignoreTable variable instead of simply writing params.FilterDescendantsInstances = ignoreParts? There isn’t a need to create a new variable with the same value as what is passed into the function.

Also, try adding a few more print statements here and there to narrow down the problem. Let me know what changes.

Thanks for your reply! I’ll try what you said and I’ll tell you if it works!

I put the variables in the loop, and changed the IgnoreTable by IgnoreParts, but it doesn’t change anything, I still dont detect parts in the workspace. :confused:

Please add more print statements in various places like the while...do loop, inside the raycast function, etc. Let me know what gets printed to the output.

I solved the problem by replacing the while loop by RunService, I don’t know why this work and not the while loop

btw I added some print statement, when there is no result, when there is an instance found, and it was only printing “No Result”

1 Like