Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I make a player detect another player on different teams?

Asked by 4 years ago

I am developing a horror game where the survivors have to hide and survive from a monster. If a monster is in contact with a player or facing towards a player within a distance, it will trigger a warning or whatever to that specific survivor to run. This is based on PvP (not PvE) teams.

I have done research of Raycasting and I still don't understand about it. I would appreciate if anyone explain or sample a code for me.

This is my code.

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

2 answers

Log in to vote
0
Answered by
ryan32t 306 Moderation Voter
4 years ago
Edited 4 years ago

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")

0
I'll try out the example you've given me. Thank you! User#27966 0 — 4y
0
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? User#27966 0 — 4y
0
Check my last Edit. It tells you how i did it ryan32t 306 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

I still don't understand and sorry if I am asking too much. I really suck at this, but here's my code:

local Camera = workspace.CurrentCamera
local part = workspace.part
local castPoints = {part}
local ignoreList = {}


local detection, isVisible = Camera:WorldToScreenPoint(part.Position)
local visibleTrue = Camera:GetPartsObscuringTarget(castPoints, ignoreList)

if isVisible then
    if visibleTrue then
        print("Player sees Part.")
    else
        print("The part is no longer visible.")
    end
end

Answer this question