For my game, there are a few situations where I need players to be able to tell if they can see another player. For example, if the player playing as the killer sees a survivor, it should initiate chase events and so on. What would be the best way to approach this? I thought about running a function every time the player’s camera cframe changes, which would loop through all players and if they are close enough and on screen it would add them to a table and then the table would be fired to the server for the server to handle, but I’m worried that this might not be the most efficient way to approach this especially when there could be survivor characters who have abilities like this as well. Here is the code I have created for the client.
local playersSeen = {}
camera:GetPropertyChangedSignal("CFrame"):Connect(function()
for _, player in pairs(game.Players:GetChildren()) do
if player.Character then
if isPointVisible(player.Character.Head.Position) then
playersSeen[player.Name] = true
else
playersSeen[player.Name] = false
end
end
end
remotes.viewUpdate:FireServer(playersSeen)
end)
and then for the server
local playerVisions = {}
remotes.viewUpdate.OnServerEvent:Connect(function(player, visiblePlayers)
for playerName, state in pairs(visiblePlayers) do
local foundPlayer = game.Players:FindFirstChild(playerName)
if foundPlayer then
if state == true and playerVisions[player] == false then --to make sure the events only fire when nescessary
local foundCharacter = foundPlayer.Character
if foundCharacter and player.Character then
if (player.Character.HumanoidRootPart.Position - foundCharacter.HumanoidRootPart.Position).Magnitude <= 20 then
playerVisions[player] = true
--handle vision event start via modulescript
end
end
elseif state == false and playerVisions[player] == true then --to make sure the events only fire when nescessary
playerVisions[player] = false
--handle vision event end via modulescript
end
end
end
end)
My main concern with this is that if all 9 players in the round are having their visions tracked, I don’t want it to overload and cause performance issues and stuff. If anyone has any ideas how to handle this better/clean it up that would be much appreciated.
Also I apologize if the code is messy! I wrote it up quickly to get my point across. ^^"