Checking if a player is within a certain radius of another player

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to check if a player (player 1) is within a certain radius of another player (player 2)

  2. What is the issue? Include enough details if possible!
    I don’t know how to do this

  3. What solutions have you thought of so far?
    None

You could try this!

local RADIUS = 45 -- Change it according to what you want

local function CheckRadius(p1, p2) -- Player instances
    if p1:IsA("Player") and p2:IsA("Player") then
        local char_1 = p1.Character
        local char_2 = p2.Character
        if char_1 and char_2 then
            if char_1.PrimaryPart and char_2.PrimaryPart then
                if (char_1.PrimaryPart.Position - char_2.PrimaryPart.Position).magnitude <= RADIUS then -- Checking magnitude between p1 and p2
                    return true -- Yes, they're within given radius!
                else
                    return false -- Nope, they aren't!
                end
            end
        end
    end
    return nil
end

local p1 = game.Players.Avanthyst
local p2 = game.Players.PLAYER
local success = CheckRadius(p1, p2)

if success then
    print("Yup!")
end
1 Like