Your script currently has nothing to do with RayTracing and it seems like the goal of your script is to see if the player is visible or not. and right now your script isn't achieving your goal. and if you want find the player in different team then just do:
if player.TeamColor ~= game.Players.LocalPlayer.TeamColor then
end
Now i'm fixing your script:
Your Script:
local Camera = workspace.CurrentCamera
local part = workspace.Part
local detection = Camera:WorldToScreenPoint(part.Position)
if detection then
for _, player in pairs(game.Players:GetPlayers()) do
player.TeamColor = BrickColor.new("Black")
script.Parent.Sound.Playing = true
print("I see you!")
end
end
you should have two variables to Camera:WorldToScreenPoint():
local detection,isVisible = Camera:WorldToScreenPoint(part.Position)
then in your original script in the If Then Statement change detection to isVisible:
local Camera = workspace.CurrentCamera
local part = workspace.Part
local detection,isVisible = Camera:WorldToScreenPoint(part.Position)
if isVisible then
for _, player in pairs(game.Players:GetPlayers()) do
player.TeamColor = BrickColor.new("Black")
script.Parent.Sound.Playing = true
print("I see you!")
end
end
How the script works should be self-explanatory now, right?
because the isVisible variable comes after detection. And isVisible should be self-explanatory.
But the problem with what you're trying to accomplish is that, every part that is within the Camera will make isVisible true even if you can't see the part because a wall is in the way..
To detect if the monster is looking at the player. You can use GetPartsObscuringTarget on Heartbeat to check if there are any parts obscuring the monster's view and if there are none then the monster is in fact, staring at the player.
EDIT: Hi, I'm back. It's been an hour after posting this Answer. I have been trying to Detect All Players In FOV for two days now. And I created a script that made it possible to detect player's in FOV. I won't give you the code but I'll give 3 steps and you will have to figure out the rest. Good Luck!
**(1)**Local Script
**(2)**WorldToScreenPoint
**(3)**GetPartsObscuringTarget
EDIT: Sure. All the code I had was in local script. I used WorldToScreenPoint to detect all visible parts even through walls. Then I used GetPartsObscuringTarget to detect if there are any parts blocking the local player's view from the part and if there are not any parts blocking the view then print("Player sees Part")
3 comments
I'll try out the example you've given me. Thank you!
I tried it but I still don't understand it. Is it okay if you give me a tiny hint so I can be on the right track?
Check my last Edit. It tells you how i did it