How to find player closest to the mouse and reference them

How did the script work? @majaed06

I got an error “Attempt the compare number < nill”

Hold on, I didn’t test yours yet.

What line was the error at? It should say a number in console

it was line 26. not sure why tho.

Let me know how my script works

Fixed code:

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 not closestPos then 
                closestPos = mag
                closestplr = v
                continue
            end
            if  mag < closestpos then
                closestplr = v
                closestpos = mag
            end
        end
    end
    
    print(closestplr.Name)
end)
1 Like

Worked for a second, just said my roblox name. Then i got the attempt to compare number < nil

Then try this for the local script:

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

local localPlayer = Players.LocalPlayer
local mouse = localPlayer:GetMouse ()

local maxDistance = 10

UserInputService.InputBegan:Connect(function (input,gp)
    if gp then return end
    local nearestPlayer, nearestDistance = nil,9999
    if input.KeyCode == Enum.KeyCode.E then
        local mousePosition = mouse.Hit.p
        for _, player in pairs(Players:GetPlayers()) do
            if player ~= localPlayer then
                local character = player.Character
                local distance = player:DistanceFromCharacter(mousePosition)
                if not character or distance > maxDistance or
                (nearestDistance and distance >= nearestDistance) then
                    continue
                end
                nearestDistance = distance
                nearestPlayer = player
            end
        end
        if nearestPlayer then
        -- do whatever to nearest player
        end
    end
end)

Error at line 17 "Expected ‘then’ when parsing if statement, got ‘distance’

That was my bad, I’m typing this on mobile. Try recopying it now

Error on line 18 now. Expected ‘)’ (to close ‘(’ at column 5), got ‘distance’

Try it again, autocorrect makes trying to type code on mobile the most frustrating thing ever.

Ok, didnt get any errors until i clicked E. attempt to compare nil <= number Line 18

you can assume what this reply is for

Wait does that mean theres no player getting detected if so i feel dumb lol

Nevermind I fixed the issue and theres no more errors

Most likely, although in that case it should be printing your name. Where I have

-- do whatever to nearest player

Put:

print(nearestPlayer.Name)

May I ask what you fixed so I can update it in my code for other people who might the same issue?

Now theres an error ;-; line 24 (Where i put the print at) attempt to index nil with ‘Name’