Help to use locate a player using magnitude

I currently want to know the code that will let me locate the player and use it to actually insert it in one magnitude script I’m planning to do, anyone have any idea of how I can do that?

Im not quite sure what you are asking. Why would you need to use magnitude to find the players position? just use CFrame/Position

I’m trying to make a pop up gui that opens when a player gets close

to get the magnitude between two parts you would do (parta.Position - partb.Position).magnitude

Exactly, but I need to know how to get the position of the player first

you could use a localscript to call a RemoteEvent that contains the player’s position to the server which would do whatever you need it to do with the position

Exactly, but I don’t know HOW to code that, to get the player

I always end up with

local player = game.Players.LocalPlayer
And don’t know what to do next

local player = game.Players.LocalPlayer
local character = player.Character

then inside character grab whatever part you want to get the position from
example : character.Head.Position

1 Like

Alright, I’ll try that then now.

You could just do this:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character
local RenderDist = 10

while true do
    wait(1)
    local Hrp = Character:FindFirstChild("HumanoidRootPart")
    
    local Magnitude = (SomePart.Position - Hrp.Position).Magnitude

    if Magnitude <= RenderDist then
        -- Make the GUI appear yourself because... scripting support is not for requesting people to make scripts for you!
    end
end

Put this as a localscript inside the GUI.

Try searching for these kinds of things first. A quick search can always reveal quite a bit and help you towards what you’re looking for. Your thread title is also a little confusing - it says “locate a player using magnitude” but you want the LocalPlayer’s character’s distance from something.

A quick search brought me to Player.DistanceFromCharacter. You can use this to check the distance magnitude between a Vector3 and the player’s character. Handle all cases that are non-zero or below the maximum distance that you’re checking for.

Sample code:

local RunService = game:GetService("RunService")

local LocalPlayer = game:GetService("Players").LocalPlayer
local Checkpoint = workspace:WaitForChild("Checkpoint") -- Or whatever

local MAXIMUM_DISTANCE = 20

while true do -- Non-terminating loop. Can also use RunService?
    local distance = LocalPlayer:DistanceFromCharacter(Checkpoint.Position)
    if distance < 0 and distance >= MAXIMUM_DISTANCE then
        -- Do something
    end
    -- RunService can run this every frame. Just yield as minimally as possible.
    wait(0.5)
end

In case you’re wondering, this is the distance between a character’s head and a point. It handles the work on the backend instead of you doing the math and positional check. Plus, if the behaviour updates, the change is pretty much invisible to you.

I say don’t handle cases when the magnitude is zero because that means the player has no character or head and you can get unexpected results if you perform actions when the distance is 0.

3 Likes