You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I am trying to figure out how to make a variable for all of the players except the local player, or the player who fired the script. Its basically for a detection system to detect if someone is strong or not. It’ll look something like this system, but with an actual enemy player’s character.
Video:
-
What is the issue? Include screenshots / videos if possible!
I’m having trouble figuring out a way to make a variable for my player and the rest of players, likewise with my character, and the rest of the character.
SERVERSCRIPT:
-- Import our services
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
-- Identify our target for use in the function below
-- Variables
local players = game:GetService("Players")
local playersTable = players:GetPlayers()
local plr = players.LocalPlayer
local MyCharacter = plr.Character or plr.CharacterAdded:Wait()
for i,player in pairs(playersTable) do
if player.Name ~= plr.Name then continue end
table.remove(playersTable,i)
EnemyCharacter = player.Character
EnemyCharacterRootPart = EnemyCharacter.HumanoidRootPart
break
end
--Making sure MyPlayer Is excluded from the rest
-- This is not technically needed, but it is likely preferable as we do not want to our player model obscuring the part
-- to count as an obstruction.
local RaycastParameters = RaycastParams.new()
RaycastParameters.FilterDescendantsInstances = {MyCharacter}
local Camera = workspace.CurrentCamera
local TargetOnScreen = false
local TargetVisible = false
-- Primary function
local function checkIfPartIsOnScreen(EnemyHMRP)
-- Use :WorldToViewportPoint
local _, IsOnScreen = Camera:WorldToViewportPoint(EnemyHMRP.Position)
-- Check if our target is on screen, and update our variable informing us if it is.
if IsOnScreen and (not TargetOnScreen) then
TargetOnScreen = true
print("The target is now in our FOV!")
-- Do the same, but in reverse
elseif (not IsOnScreen) and TargetOnScreen then
TargetOnScreen = false
-- If the target is off screen, then the target must also not be visible.
TargetVisible = false
print("The target is no longer in our FOV.")
end
-- We don't need to perform a raycast check if the target is out of our FOV
if TargetOnScreen then
-- This may be confusing, but it is Vector3 math. It is getting the direction between our Camera and the part.
local Direction = (EnemyCharacterRootPart.Position - Camera.CFrame.Position).Unit
-- This 200 could be customized to set a specific range that you are checking for the player in, though performing
-- a separate magnitude check beforehand will likely be more optimized.
local RaycastResult = workspace:Raycast(Camera.CFrame.Position, Direction * 200, RaycastParameters)
-- Check if our part was not hit by our Raycast check!
if (not RaycastResult) or RaycastResult.Instance ~= EnemyCharacterRootPart then
-- Now that we know that the target is obscured, we need to check if we need to output!
if TargetVisible then
TargetVisible = false
print("The target is now obscured.")
end
else
-- If the above is not true, we can see the target.
if (not TargetVisible) then
TargetVisible = true
print("The target is visible.")
end
end
end
end
-- Run our check every frame, you may want to not do this depending on how much you are running with this check.
RunService.RenderStepped:Connect(function()
checkIfPartIsOnScreen(EnemyCharacterRootPart)
end)