Can't get my ray trace code to run

local camera = game.Workspace.CurrentCamera
local direction = (game.Players.LocalPlayer.Character.Head.Position - camera.CFrame.Position).Unit * 100
print(“Test”)
game:GetService(“RunService”).RenderStepped:Connect(function()
local ray = Ray.new(
camera.CFrame,
direction
)
local ignore = game.Players.LocalPlayer.Character
local hit, position = workspace:FindPartOnRay(ray, ignore)

if hit and hit:IsA("BasePart") then
	hit.Transparency = 0.5
end	

end)

The error I get is: 12:52:54.478 Infinite yield possible on ‘Players.matt1020304050:WaitForChild(“Character”)’ - Studio

It’s like it doesn’t want to load the character?

Is this the script that’s giving the infinite yield warn? I don’t see any :WaitForChild() on the script you’ve provided.

2 Likes

I don’t see any WaitForChild on the script, so not sure if this is the script warning the error, but here is some information.
WaitForChild(). Does not check for properties. Character is a property of Player, not a child. If you really want a safe way to get the player’s character, do:

local Char = player.Character or player.CharacterAdded:Wait()

I figured it out, I had a waitforchild but it didn’t work, but yea I did the → CharacterAdded:wait()

local camera = game.Workspace.CurrentCamera
local myCharacter = game.Players.LocalPlayer.Character
if not myCharacter or not myCharacter.Parent then
	character = game.Players.LocalPlayer.CharacterAdded:wait()
end
local direction = (game.Players.LocalPlayer.Character:WaitForChild("Head").Position - camera.CFrame.Position).Unit * 500
print("Test")
game:GetService("RunService").RenderStepped:Connect(function()
	local ray = Ray.new(
		camera.CFrame.Position,
		direction
	)
	
	local ignore = game.Players.LocalPlayer.Character 
	local hit, position = workspace:FindPartOnRay(ray, ignore)

	if hit and hit:IsA("BasePart") then
		hit.Transparency = 1 
		
	end	
end)

My next question is, I want to try and make all parts invisible that are inside my players camera view. How would I do that? Like how do I check if everything inside the camera and make it invisible.

Mycharacter should just be plr.Char or chradded:Wait(). In lua, if the variable has an or statement, and the first variable is nil, It moves on to the second variable.

The cam view thing should be posted in a separate devforum post.

I put it in a variable so it’s easier to read. Otherwise it will end up on separate lines.