How would you see if a player is facing a model?

You would probably use raycasting, but unlike most y’all, raycasting is not my thing. I have only tried out raycasting a couple of times. I don’t need entire script, but maybe things like what kind of things I would be using, thanks!

4 Likes

I think you could use CFrame.LookVector? But i don’t know.

1 Like

And you would make a the ray on the characters head right?

1 Like

I think so, i only recently learned about rays.

1 Like

how would you do that though?(30 chars)

1 Like

https://developer.roblox.com/en-us/api-reference/function/Camera/WorldToViewportPoint

It contains a code example that will definitely help you out. It is at the bottom.

It uses the old raycasting API but switching over to the new one isn’t too hard.

local Workspace = game:GetService("Workspace")
local camera = Workspace.CurrentCamera

local function is_point_visible(world_point)
    local vector, on_screen = camera:WorldToViewportPoint(world_point)
    
    if on_screen then
        local origin = camera.CFrame.Position
        local raycast_result = Workspace:Raycast(origin, world_point - origin)
        local hit = raycast_result and raycast_result.Instance
        if hit then
            return false
        end
    else
        return false
    end
    return true
end
1 Like

I know you would do this in a serverscript, but instead of that would you do:

game.Players.PlayerAdded:Connect(function(plr)
local origin = plr.Character.Head
end)
1 Like

Camera API should be used in a local script.

1 Like

What I want to do is make a ray on the players head though…

Then you would do

local head = player.Character.Head
local raycast_result = workspace:Raycast(head.Position, head.CFrame.LookVector*500)

This will cast a ray 500 studs forward from the head’s position, but you can lower it, or raise it (max ray length is 5000 iirc)

I have a question, does the ray that starts at the players head go forward of the head, or forward and back of the head, or just the back of the head, I only want the first one

The ray starts at the head. Then it goes 500 studs forward. Visual:

1 Like

So then it would look like this?

game.Players.PlayerAdded:Connect(function(plr)
    local head = plr.Character.Head
    local raycast_result = workspace:Raycast(head.Position, head.CFrame.LookVector*500)
end)
1 Like

Yes but the character might not have loaded in the instant the player might have. Use Player.CharacterAdded to detect when the character is spawned in

1 Like
game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function()
    local head = plr.Character.Head
    local raycast_result = workspace:Raycast(head.Position, head.CFrame.LookVector*500)
    end)
end)
1 Like

Ok I tried this but this is what I got: 14:22:40.803 - ServerScriptService.Ray:5: attempt to index nil with ‘Instance’

-- Serverscript in serverscriptservice
game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function()
       local head = plr.Character.Head
        local raycast_result = workspace:Raycast(head.Position, head.CFrame.LookVector*500)
       local hitPart = raycast_result.Instance
       if hitPart.Name == "Chair" then
          print("Looking at chair")
       else
          print("Not looking at chair")
       end
    end)
end)
1 Like

WorldRoot:Raycast returns nil if the ray didn’t hit anything.

if raycast_result then
    local hit_part = raycast_result.Instance

    if hit_part.Name == "Chair" then
        print("Looking at chair")
    else
        print("Not looking at chair")
    end
end
1 Like

No errors, but even though I am clearly looking at the chair, its not printing “Looking at chair” and it doesn’t print “Not looking at chair” when i look away, script:

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function()
       local head = plr.Character.Head
        local raycast_result = workspace:Raycast(head.Position, head.CFrame.LookVector*500)
        if raycast_result then
    local hit_part = raycast_result.Instance

    if hit_part.Name == "Chair" then
        print("Looking at chair")
    else
        print("Not looking at chair")
     end
   end
  end)
end)
1 Like

Because it is casting 500 studs straight. This is why I recommended the raycasting from the camera but in a local script.

1 Like

how do you do that though , this thing?

local function isPointVisible(worldPoint)
    local camera = workspace.CurrentCamera
    local vector, onScreen = camera:WorldToViewportPoint(worldPoint)
    
    if onScreen then
        local origin = camera.CFrame.p
        local ray = Ray.new(origin, worldPoint - origin)
        local hit = workspace:FindPartOnRay(ray)
        if hit then
            return false
        end
    else
        return false
    end
    return true
end

Then how would you start the ray at the head then?

1 Like