I’m trying to make a system that detects players within a part’s radius, but only if they are visible. If a player is within the radius but obscured (i.e., not in the part’s line of sight), they shouldn’t be detected. For clarification, you can refer to the video I made based on Pressure. I attempted to use Rays for detection, but they weren’t large enough, and using a large number of rays would be inefficient. Does anyone have suggestions for a better way to implement this system?
You do want to use rays since they’re the best way to find out if the player is out in the open
-- Ima use 2 functions here for organization purposes. Combine them if you wish.
local detector = --- Your detector part here
local md = --- Max radius here
function PlayersInRadius()
local playersinrange = {}
for _,plr in pairs(game.Players:GetPlayers()) do
if (detector. Position - plr.Character.HumanoidRootPart.Position).Magnitude < md then
table.insert(playersinrange,plr.Character)
end
end
return playersinrange --- Returning table to loop through
end
function AttackPlayer()
local pir = PlayersInRadius()
for _,char in pairs(pir) do
local rp = RaycastParams.new()
rp.FilterDescendantsInstances = {--- Add the parts affiliated with the detector here}
rp.FilterType = Enum.RaycastFilterType.Exclude
local ray = workspace:Raycast(detector.Position,(detector.Position - char.HumanoidRootPart.Position).Unit * 1000,rp)
if ray and ray.Instance:IsDescendantOf(char) then
char:BreakJoints()
end
end
end
Sorry but, i dont really understand your script.
What your doing is your checking the distance between the player and the part? The rest i dont quite understand.
maybe you could first detect players in the area and then send a ray from the player to the target to see if they are visible to the target?
Ill explain the AttackPlayer function to you:
local pir = PlayersInRadius()
This line is returning an array of players that are in the range of the detector for us to loop through
local rp = RaycastParams.new()
rp.FilterDescendantsInstances = {--- Add the parts affiliated with the detector here}
rp.FilterType = Enum.RaycastFilterType.Exclude
local ray = workspace:Raycast(detector.Position,(detector.Position - char.HumanoidRootPart.Position).Unit * 1000,rp)
The first line creates an empty RaycastParams. This is something we use when we want to whitelist or blacklist specific parts in the workspace, since the ray returns the first object it hits only. We shouldn’t whitelist the character only or else the ray will ignore everything else until it hits the character. So we ‘Exclude’ any parts that the detector is surrounded with so the ray will ignore them. Now we are actually firing the ray towards the player from the detector’s position for a total of 1000 studs. If the ray returns the character, that means that the character is not hiding and you can kill them. If the ray returns another part like the chair, that means the character is hidden.
if ray and ray.Instance:IsDescendantOf(char) then
char:BreakJoints()
end
This checks if the ray returned the character or not, since the ray would prob hit a descendant of the character
Extra
I see that there’s a beam travelling around the map. If you want to account for that, then simply do change the origin (1st arg) in the raycasting line:
workspace:Raycast(detector.Position + (detector.CFrame.LookVector * 10))
This basically fires the ray from the front of the part, since i assume that’s where the spotlight is facing. Also do not filter out the detector part for this to work
I think i understand your ideia now,
in the AttackPlayer function your putting the direction to the player, and extending the ray by 1000 by transforming it to a .Unit and multiplying it by 1000.
i also think bluedood ideia works, so im gonna test them both and see which one works the best.
thanks alot for helping me make this system (sorry if i sound dumb in a beginner scripter & this is my first time using the forum lol)
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.