Detect player when the player is in another player Fov

  1. What do you want to achieve?
    so I want to make a when a player(killer) is in your fov , your value(chasing time) will increase .
  2. What is the issue?
    i was planning to use magnitude and world to screen point , but i dont quite understand the world to screen point function . So can anyone show some example script of how it work ?

I was trying something like this. You can work with Raycasts and WorldToViewportPoint.

1 Like

WorldToViewportPoint takes one argument, the Vector3 (position) of whatever you are attempting to transfer to on screen coordinates. As mentioned in the documentation. It returns two arguments as a tuple, the first being a Vector3 where the X and Y represent a Vector2 of the screen (horizontal and vertical in pixels,) and a Z value which is the depth in studs. This first value can be ignored.

The second, and more important value is a boolean describing whether or not the Vector3 position is in the player’s FOV. This can be used along with a raycasting check to see if something is on screen. Here is a StarterCharacter LocalScript that performs this, with hopefully informative comments:

-- Identify our target for use in the function below
local Target = workspace:WaitForChild("RedPart")

-- Import our services
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

-- Variables
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
-- This is not technically needed, but it is likely preferable as we do not want to our player model obscuring the part
-- to count as an obstruction.
local RaycastParameters = RaycastParams.new()
RaycastParameters.FilterDescendantsInstances = {Character}

local Camera = workspace.CurrentCamera
local TargetOnScreen = false
local TargetVisible = false

-- Primary function
local function checkIfPartIsOnScreen(Part)
	-- Use :WorldToViewportPoint
	local _, IsOnScreen = Camera:WorldToViewportPoint(Part.Position)
	
	-- Check if our target is on screen, and update our variable informing us if it is.
	if IsOnScreen and (not TargetOnScreen) then
		TargetOnScreen = true
		print("The target is now in our FOV!")
		-- Do the same, but in reverse
	elseif (not IsOnScreen) and TargetOnScreen then
		TargetOnScreen = false
		-- If the target is off screen, then the target must also not be visible.
		TargetVisible = false
		print("The target is no longer in our FOV.")
	end
	
	-- We don't need to perform a raycast check if the target is out of our FOV
	if TargetOnScreen then
		
		-- This may be confusing, but it is Vector3 math. It is getting the direction between our Camera and the part.
		local Direction = (Target.Position - Camera.CFrame.Position).Unit
		
		-- This 200 could be customized to set a specific range that you are checking for the player in, though performing
		-- a separate magnitude check beforehand will likely be more optimized.
		local RaycastResult = workspace:Raycast(Camera.CFrame.Position, Direction * 200, RaycastParameters)
		
		-- Check if our part was not hit by our Raycast check!
		if (not RaycastResult) or RaycastResult.Instance ~= Target then
			-- Now that we know that the target is obscured, we need to check if we need to output!
			if TargetVisible then
				TargetVisible = false
				print("The target is now obscured.")
			end
		else
			-- If the above is not true, we can see the target.
			if (not TargetVisible) then
				TargetVisible = true
				print("The target is visible.")
			end
		end
	end
end

-- Run our check every frame, you may want to not do this depending on how much you are running with this check.
RunService.RenderStepped:Connect(function()
	checkIfPartIsOnScreen(Target)
end)

If you still are confused, I have uploaded this to a place:

If you are confused, or have any questions, please ask. I will answer them as soon as I am back online :slightly_smiling_face:

1 Like

If the target is player , how I’m gonna type the variable ??

local Players = game:GetService(“Players”)

This gives you access to list of the Players…

But i GUESS THAT YOUR ARe GOING TO HAVE TO fire a Remote event from the server to each Client to let the client know who the Target is…
And guessing you would pick one of that Player’s parts (Torso) to search on…

But, I don’t know.

Giving an A+ to aidmaster for commenting his script. Easy to read.

Oh, I guess all this would have to be done on the Server? So no hacking…

i mean player “character” , and why do u need it on the server??

-- Import our services
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

-- Variables
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
-- This is not technically needed, but it is likely preferable as we do not want to our player model obscuring the part
-- to count as an obstruction.

Game.Players.PlayerAdded:Connect(function()
for i, v in pairs(game.Workspace:GetChildren) do
     if v:IsA("Model") and v:WaitForChild("Humanoid") then
         local Magnitudes = (v.HumanoidRootPart.Position - Character.HumanoidRootPart.Position).Magnitude

     if Magnitudes < 10 then
       local Target = v
       end
    end
end)

local RaycastParameters = RaycastParams.new()
RaycastParameters.FilterDescendantsInstances = {Character}

local Camera = workspace.CurrentCamera
local TargetOnScreen = false
local TargetVisible = false

-- Primary function
local function checkIfPartIsOnScreen(Part)
	-- Use :WorldToViewportPoint
	local _, IsOnScreen = Camera:WorldToViewportPoint(Part.Position)
	
	-- Check if our target is on screen, and update our variable informing us if it is.
	if IsOnScreen and (not TargetOnScreen) then
		TargetOnScreen = true
		print("The target is now in our FOV!")
		-- Do the same, but in reverse
	elseif (not IsOnScreen) and TargetOnScreen then
		TargetOnScreen = false
		-- If the target is off screen, then the target must also not be visible.
		TargetVisible = false
		print("The target is no longer in our FOV.")
	end
	
	-- We don't need to perform a raycast check if the target is out of our FOV
	if TargetOnScreen then
		
		-- This may be confusing, but it is Vector3 math. It is getting the direction between our Camera and the part.
		local Direction = (Target.Position - Camera.CFrame.Position).Unit
		
		-- This 200 could be customized to set a specific range that you are checking for the player in, though performing
		-- a separate magnitude check beforehand will likely be more optimized.
		local RaycastResult = workspace:Raycast(Camera.CFrame.Position, Direction * 200, RaycastParameters)
		
		-- Check if our part was not hit by our Raycast check!
		if (not RaycastResult) or RaycastResult.Instance ~= Target then
			-- Now that we know that the target is obscured, we need to check if we need to output!
			if TargetVisible then
				TargetVisible = false
				print("The target is now obscured.")
			end
		else
			-- If the above is not true, we can see the target.
			if (not TargetVisible) then
				TargetVisible = true
				print("The target is visible.")
			end
		end
	end
end

-- Run our check every frame, you may want to not do this depending on how much you are running with this check.
RunService.RenderStepped:Connect(function()
	checkIfPartIsOnScreen(Target)
end)

i actually dont know how i gonna make the target variable lol . how i gonna change this to player ??