Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you use WorldToScreenPoint to see players?

I was wondering if I can use WorldToScreenPoint to see a player. I tried using a loop to detect parts of a player instead, but nothing worked.

local Camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Character = workspace:WaitForChild(Player.Name)

while true do
    local Vector, InScreen = Camera:WorldToScreenPoint(Character)

    if InScreen then
		for i, v in pairs(Character:GetChildren()) do
			print("A Player has been detected!")
		end
	else
		print("You're away!")
    end
	wait(1)
end

1 answer

✓ Accepted answer

WorldToScreenPoint() can check if a Part's Position is within the scope of the Camera, whether it is physically visible or not. As long as it is within the scope.

Example:

-- Variables
local cam = workspace.CurrentCamera
local plrs = game:GetService("Players")
local plr = plrs.LocalPlayer
local char = workspace:WaitForChild(plr.Name)
local hpart = char:WaitForChild("HumanoidRootPart")

-- Code
game:GetService("RunService").Heartbeat:Connect(function()
	local instance,inScope = cam:WorldToScreenPoint(hpart.Position)
	if inScope == true then
		print(plr.Name .. "'s ".. hpart.Name .. " is in scope.")
	end
end)

7 comments

0

I am getting an error on line 10 repeatedly. The error is "Unable to cast Instance to Vector3?"

0

Make sure the code says "instance,inscope = cam..." notice the lowercase "i" in "instance" if you use "Instance" then you're doing something else

0

It is lowercase, but it's still giving me a strange error.

0

Nevermind, I fixed it! I think you've missed .Position on line 10. I hope it's working now.

0

oh you're right! haha! sorry!

0

Hmmm... This is strange again. I am supposed to look at a different player humanoid part, not myself. How can I change this?

0

That is irrelevant to the post. Msg me on discord. ryan32t#6658

This discussion is preserved as it was. New answers, comments, and votes are closed.