How to make something happen if the player's face is looked at

  1. I want to make something happen (such as a RemoteEvent being fired) if the player’s face is looked at. (like SCP-096 but SCP-096 is the player)

  2. I really can’t understand raycasting well and need some help with that.

  3. I have looked everywhere to see if something could help me but nothing yet and like said before I really don’t understand raycasting.

What I think would be the best way to execute it. (a local script inside startercharacterscripts or inside the character model)

local value = true

while true do
	wait()
	if player face is looked at and value == true then
		remoteevent:fire()
		value = false
	end
end
3 Likes

Use Player.Character.Head and Player:GetMouse() function with a little bit touch of CFraming.
local script in starterCharacterScript

local character = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()

local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")

if character or mouse == nil then return end

mouse.Move:Connect(function()
    remote:FireServer(mouse.Hit.Position)
end)

Script in serverScriptService

local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local visualize = false -- change to true to see ray.

remote.onServerEvent:Connect(function(player, mouse)
    local character = player.Character
    local humanoid = character:WaitForChild("Humanoid")
    local head = character:WaitForChild("Head")

    if humanoid.Health > 0 then
          local origin = head.Position
          local direction = (mouse - origin).Unit*300
          local result = game.Workspace:Raycast(origin, direction)

          local intersection = result and result.Position or origin + direction
          local distance = (origin - intersection).Magnitude

          if visualize == true then
            local g = Instance.new("Part")
		    g.Size = Vector3.new(0.1, 0.1, distance)
		    g.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
		    g.Parent = game.Workspace
		    g.Anchored = true
		    g.CanCollide = false
		    g.Color = Color3.new(0.317647, 1, 0)
		    g.Material = Enum.Material.Neon

            game.Debris:AddItem(g, 0.5)
         end

        print(result.Instance)
   end
end)
1 Like

Thank you, I will try that soon and keep you updated

1 Like

What is “result.Instance”?? (sorry if this is a really dumb question)

1 Like

It’s the name of the instace the result hit.

1 Like

Is it normal nothing is happening even with the visualizer on and nothing gets printed??

1 Like

I’m currently fixing my past script. Please be patient.

Sorry, inform me once you are sure it is fully fixed so I avoid doing this again

1 Like

local script in StarterPlayerScript :

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local remote = game.ReplicatedStorage:WaitForChild("MouseEvent")

mouse.Move:Connect(function()
	remote:FireServer(mouse.Hit.Position)
end)

script in serverScriptService :

local remote = game.ReplicatedStorage:WaitForChild("MouseEvent")
local visualize = true -- change to true to see the ray(s), false to not see ray(s)

local origin = nil
local direction = nil
local intersection = nil
local result = nil
local distance = nil

local range = 250

remote.OnServerEvent:Connect(function(player, mousePos)
	print(player, mousePos)
	
	local character = player.Character
	local humanoid = character:WaitForChild("Humanoid")
	local head = character:WaitForChild("Head")
	
	if humanoid.Health > 0 and head ~= nil then
		origin = head.Position
		direction = (mousePos - origin).Unit * range
		result = game.Workspace:Raycast(origin, direction)

		intersection = result and result.Position or origin + direction
		distance = (origin - intersection).Magnitude
		
		if visualize == true then
			local ray = Instance.new("Part")
		    ray.Size = Vector3.new(0.1, 0.1, distance)
		    ray.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)
		    ray.Parent = game.Workspace
		    ray.Anchored = true
		    ray.CanCollide = false
		    ray.Color = Color3.new(1, 0, 0)
		    ray.Material = Enum.Material.Neon

		    game.Debris:AddItem(ray, 0.25)
		end

		if result ~= nil then
			print(result.Instance) -- change to what you want to detect
			
			if result.Instance == "SCP 096" then -- in game.Workspace, this will detect the EXACT NAME OF what you want to detect
				print("Detected!", typeof(result.Instance))
			end
		end
	end	
end)

Make sure there is an remote event named “MouseEvent” in replicateStorage

3 Likes

This works wonderfully thank you

1 Like

One small improvement is to add debounce, sanity check and more to improve the system.
Made mine that when a ray is fired to the head of enemy rig, the enemy will follow the player.Character

if result ~= nil then	
			if result.Instance.Name == "Head" then -- this refer to anything that are name Head!
				local enemyCharacter = result.Instance.Parent
				
				if enemyCharacter ~= nil then
					local enemyHumanoid = enemyCharacter:FindFirstChild("Humanoid")
					if enemyHumanoid and enemyHumanoid.Health > 0 then
						enemyHumanoid:MoveTo(character.HumanoidRootPart.Position)
					end
				end
			end
		end

SCP096Basic.rbxl (57.8 KB)

1 Like

I am making it so that a script inside the instance’s parent (in this case the scp 096 character) gets enabled and fires a remote event but thank you very much

1 Like

Um so it was working not so long ago but now it isnt and gives me this error


(Nevermind figured out the issue)

How would I go on to detect a part that is inside a model
(for example the detect part inside the scp 096 model)
like do something like getdescendants and if descendant name is blabla then it does this
i tried doing that but i would just break the script
(nvm i’m dumb)

I have one last question, if i’m in first person the ray gets stuck into the player’s head how would I go on to avoid making that happen as I am making a first person game. (sorry for the responses)

(Nevermind was an issue with studio I don’t know what made it happen tho)

Is there a way I could make the ray always fire without even moving the mouse

1 Like
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local remote = game.ReplicatedStorage:WaitForChild("MouseEvent")

game["Run Service"].RenderStepped:Connect(function(dt)
	remote:FireServer(mouse.Hit.Position)
end)
1 Like

OH WOW, I definetly didn’t think of using renderstepped sorry if I bothered you my fault

Sorry yet again but is there a way the mouse can touch things that are you cannot collide into (for examples parts that have canCollide off)

1 Like

Yes. Doesn’t it detect the instance either it have CanCollide turned on of off?