I got this from the devforum, and wanted to try it with remoteevents as remotefunctions can be abusable by exploiters, I’m not sure if this would work or not, this is what I am trying to do.
LocalScript:
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Camera = workspace.CurrentCamera
local function GetClosestPlayer()
local Closest = {nil, nil}
local MousePos = Vector2.new(Mouse.X, Mouse.Y)
for _, Player in pairs(game.Players:GetPlayers()) do
if Player == game.Players.LocalPlayer then continue end
local Character = Player.Character
if Character then
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
if HumanoidRootPart then
local vector, onScreen = Camera:WorldToScreenPoint(HumanoidRootPart.Position)
if onScreen then
local Distance = (MousePos - Vector2.new(vector.X, vector.Y)).Magnitude
if Closest[1] == nil then Closest = {Distance, Player} continue end
if Distance < Closest[1] then
Closest = {Distance, Player}
end
end
end
end
end
return Closest
end
game.ReplicatedStorage:WaitForChild("RemoteFunctions"):WaitForChild("GetTarget").OnClientEvent:Connect(GetClosestPlayer)
ServerScript:
local dist, player = game.ReplicatedStorage:WaitForChild("RemoteFunctions"):WaitForChild("GetTarget"):FireClient(player)
if dist < 200 and player then
end