How can I wait until player is facing wall?

So I got this piece of code right over here, and it looks like this:

local Player = game.Players.LocalPlayer
local HRP = Player.Character.HumanoidRootPart
local Length = 15
local Ray = Ray.new(HRP.Position, HRP.CFrame.LookVector * Length)
local Wall, HitPosition, Normal, Material = workspace:FindPartOnRay(Ray, Player.Character)
if Wall then
	print("wall in front of you")
end

It prints if you spawn right next to a wall, however it doesn’t wait until you touch a wall, and that’s my goal, so how can I accomplish this? Here is what I tried:

repeat
   wait(1)
until Wall
print("wall in front of you")

But no luck.

1 Like

You are checking whether the wall exists outside of the repeat loop so it’ll never change since it’s defined once when the script begins and never again.

so how can i fix that then because I have no idea how to approach this at this rate?

Put this line after your wait():


Wall, HitPosition, Normal, Material = workspace:FindPartOnRay(Ray, Player.Character)

what would that even accomplish? isn’t that that just restating 4 already defined variables? + it didn’t fix anything :frowning_face:

I made a slight mistake. There are two parts to your raycast.
Please try this code

local HRP = Player.Character.HumanoidRootPart
local Length = 15
local Ray = Ray.new(HRP.Position, HRP.CFrame.LookVector * Length)
local Wall, HitPosition, Normal, Material = workspace:FindPartOnRay(Ray, Player.Character)

repeat
   wait(1)

Ray = Ray.new(HRP.Position, HRP.CFrame.LookVector * Length)
Wall, HitPosition, Normal, Material = workspace:FindPartOnRay(Ray, Player.Character)

until Wall
print("wall in front of you")

It would be better if you use the new raycast method. The current one you are using are deprecated.

It throws back an error: new is not a valid member of Ray

How can I achieve the same thing with this new system then?

i switched the var name to RC rather than Ray, and it gives me a new error: “Unable to cast Dictionary to Ray”

Ray might not be the best name for your variable.
Let’s fix that:

local HRP = Player.Character.HumanoidRootPart
local Length = 15
local WallRay = Ray.new(HRP.Position, HRP.CFrame.LookVector * Length)
local Wall, HitPosition, Normal, Material = workspace:FindPartOnRay(WallRay, Player.Character)

repeat
   wait(1)

WallRay = Ray.new(HRP.Position, HRP.CFrame.LookVector * Length)
Wall, HitPosition, Normal, Material = workspace:FindPartOnRay(WallRay, Player.Character)

until Wall
print("wall in front of you")

It works! Thank you for your help and patience!