Hello! So here’s a little background information. I’m trying to make a move where the closest person to the mouse in a certain range, will get effects done to them. Such as lift them into the air and slam them into the ground. I’m not sure where to start or how to reference the player that the mouse is closest too. I’d really appreciate if you’d help me.
Is this in a local script or server script?
use mouse.Hit.p
and the players Position with .Magnitude
. This would be the best way. ex:
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
mouse.Button1Click:Connect(function()
local closestplr
local closestpos
for i,v in pairs(game.Players:GetPlayers()) do
if v.Character then
local mag = (mouse.Hit.p - v.Character.HumanoidRootPart.CFrame.Position).Magnitude
if mag < closestpos then
closestplr = v
closestpos = mag
end
end
end
print(closestplr.Name)
end)
Theres no code that actually does anything atm. Im not exactly sure where to put anything right now lol
Im working on something that might work but i need to know will this in a local script or server script
If i used that, how would i be able to reference the player that im aiming at or close to aiming at?
I would start off with a LocalScript and as @Kaid3n22 said use mouse.Hit.p and then compare it to other players positions using Magnitude
Alright, how would I compare the positions? I’m not asking for spoon feeding just don’t know how to do it at all.
this might work im not sure tho
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local maxDistance = 10
while true do
wait(1)
local nearestPlayer, nearestDistance
for _, player in pairs(Players:GetPlayers()) do
local character = player.Character
local distance = player:DistanceFromCharacter(mouse.Hit.p)
if not character or
distance > maxDistance or
(nearestDistance and distance >= nearestDistance)
then
continue
end
nearestDistance = distance
nearestPlayer = player
end
print("The nearest player is ", nearestPlayer)
end
MAKE SURE TO PUT IN A LOCAL SCRIPT
Just gonna point this out, you forgot the () after :GetMouse
Kind of what I need, I just need it to be the closest person to my mouse.
I Added a script to my first post, that should work for what you need you just need to mash it in with your other stuff.
I don’t know how lol, thats my main problem atm. I don’t know how to reference to closest person to my mouse.
Your best bet would either be to loop through every player and check if Player.Character and if so compare the distance between the mouse’s position and their humanoidrootparts position. An alternative to looping through every player would be to create a table containing every players character.
How .Magnitude works is you can do for example (Vector3.new(1,0,0) - Vector3.new(0,0,0)).Magnitude and that would be equal to 1 as it is getting the difference between the two positions and then returning the distance all in one number.
the nearestPlayer variable in the script I posted above is the player that is closest to the mouse’s position
Alright, I’ll see if it works. I’ll make sure to let you know.
Do you know how you’re gonna start these effects. Like, does a certain key have to pressed or the user clicks their mouse for the nearest player to the mouse have an effect to them?
Yes, when the player hits the key “E” it’ll pretty much lift the player closest to the mouse into the air and slam them into the ground. Finding and referencing that player is my issue right now.