Not sure how to get player from mouse

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
1 Like

Is my localscript compatible with remoteevents, or should I change how I get the target from the cursor in the localscript?

“remoteevents as remotefunctions can be abusable by exploiters,”
both are equally vulnerable

the diference is that a remote event is used when you don’t expect data back (for example a message to all clients)
a remote function does need to return exactly 1 variable
(to get data from a cliemt)

if you want to make this a 2way communication then at the end of the local script you have to fire the remote event for the server.

2 Likes