Okay, here is a Roblox LocalScript that finds the nearest player and uses raycasting to determine if there's a clear line of sight (danger - red) or if it's blocked (safe - green). This script focuses on performance by checking periodically and using efficient methods.
This script should be placed inside StarterPlayerScripts.
Explanation:
statusIndicator) is created and welded to the player's HumanoidRootPart. Its color will show the safety status.findNearestPlayer(): Iterates through all other players, calculates distances (using .Magnitude for simplicity here, could use squared distance for minor optimization), and returns the character model of the closest one within MAX_DISTANCE_CHECK.checkLineOfSight():
RaycastParams to exclude the shooter's character model from the raycast check.Workspace:Raycast from the target's head towards the local player's head.raycastResult is nil), it means a clear line of sight (Danger).localCharacter. If yes, it's a direct hit (Danger). If it hits anything else, it means an obstacle is blocking the view (Safe).updateVisualization(): Called every frame by Heartbeat to smoothly update the indicator's color based on the isSafe variable. If no player is nearby, it hides the indicator.Heartbeat connection updates the visuals smoothly.task.spawn loop runs every UPDATE_INTERVAL seconds to perform the heavier tasks: finding the nearest player and checking the line of sight. This prevents these checks from running unnecessarily every frame, improving performance.CharacterAdded and CharacterRemoving to correctly handle player respawns and despawns, ensuring the script continues to work and cleans up the indicator properly.This approach uses raycasting, which is the standard and efficient way to check for line-of-sight in Roblox, rather than NavMesh, which is designed for pathfinding character movement._