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

How do I make players detect other players with WorldToScreenPoint & GetPartsObscuringTarget?

I'm trying to do something when someone comes into the view of a player. I was given to use GetPartsObscuringTarget and WorldToScreenPoint together, but it's not working.

EDIT: This has been an issue for the past 3 days so I really need help.

This is how much I have so far:

game:GetService("RunService").RenderStepped:connect(function()
	local function XRay(castPoints, ignoreList)
		ignoreList = ignoreList or {}
		local parts = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints, ignoreList)
	 	
		for _, part in pairs(parts) do
			part.LocalTransparencyModifier = 0.75
			for _, child in pairs(part:GetChildren()) do
				if child:IsA("Decal") or child:IsA("Texture") then
					child.LocalTransparencyModifier = 0.75
				end
			end
		end
	end
end)

1 comment

0

If i helped, please accept my answer so i can get reputation. Thank You!

1 answer

✓ Accepted answer

Hello again Sir. I'm from your other Question. I think I am the only person who has accomplished this?

It looks like you're trying to accomplish an X-Ray with GetPartsObscuringTarget but your problem is that the Event, RenderStepped is constantly creating a Function but nothing is calling the Function.

Your goal is to detect a player physically in view.

Use RunService to make a Loop. In that loop. First, use WorldToScreenPart to check if player is Visible. If Player is Visible is True then. Use GetPartsObscuringTarget to check if there are no parts blocking your view from the Player. Then if the check is passed then Player is Physically in view.

This Script Should Print Every Part And MeshPart Physically In View:

game:GetService("RunService").Heartbeat:Connect(function()
	for _,object in pairs(workspace:GetDescendants()) do
		if object:IsA("Part") or object:IsA("MeshPart") then
			if not object:IsDescendantOf(game.Players.LocalPlayer.Character) then
				local object2,visible =  workspace.CurrentCamera:WorldToScreenPoint(object.Position)
				if visible == true then
					local pos1 = workspace.CurrentCamera.CFrame.Position
					local pos2 = object.Position
					local castPoints = {pos1,pos2}
					local ignoreList = {game.Players.LocalPlayer.Character, object}
					local obstructs = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints,ignoreList)
					if obstructs[1] then
						return
					elseif not obstructs[1] then
						print(object.Name .. " Has Been Detected")
					end
				elseif visible ~= true then
					return
				end
			end
		end
	end
end)

4 comments

0

I don't know how to combine the WorldToScreenPoint and GetPartsObscuringTarget. I tried some research but the wiki isn't really helpful.

0

I edited my Answer and gave you an example Code to print every physically visible part/meshpart

0

I understand the castPoints, but can you also explain the ignoreList?

0

the ignoreList is a list of instances that obstruct ignores. which means if an instance in ignoreList is blocking the castPoints then it will be ignored as if it doesn't exist

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