Is my GUI Code correct?

Hi Guys, it is the first time ever I even saw Code Review, so I don’t know much about is it the right place to post this, but the name only brought me here.

I was trying to make a GUI you must have seen in different games, basically how much far something is from our character. I made a GUI for it. I ain’t much satisfied with my code, so it would be helpful if you help me in my code and give any idea or figure out any problem. Thanks :slight_smile:

Code-

local hrp = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")

while wait(0.5) do
	local cframe = workspace.Part2.Position.Magnitude
	position = UDim2.new(character.Position.Magnitude/cframe)
	script.Parent.Frame.TextLabel:TweenPosition(position)
end

What exactly are you trying to achive? Can you provide a video? Do you just want to show the distance in studs? Or like a arrow like in Car Crusher Sim 2?

1 Like

You are misunderstanding thr concept of ‘magnitude’. What you will need to do is put the HumanoidRootPart’s POSITION into a variable, find the second object amd get the POSITION of it as well. Next, you can get the distance between the character and the target object as shown below:

local HRP = game.Players.LocalPlayer.Character:WaitForChikd("HumanoidRootPart");

function GetMagnitude()
local TargetObject = workspace.Object --your second object
local magnitude = (HRP.Position - TargetObject.Position).magnitude
return magnitude
end

For the rest of the code I’m not sure what you are trying to achieve, but this is the main thing you’ll need.

1 Like

Player:DistanceFromCharacter

local magnitude = Player:DistanceFromCharacter(TargetObject.Position)
2 Likes

create a billboardgui, make it’s size UDim2.fromScale(75,75), add something with the size UDim2.fromScale(1,1) this is an easy method, also set it’s adornee to the part you want


the good thing is that this doesn’t require scripting :wink:

1 Like

Basically I want to depict in a GUI how much far the HRP is from the Target. There would be a frame tweening relative to how much far we are from the target. Like when we are at the middle, the frame also gets at the middle, when we reached end, frame also reaches the end.

You can use a Billboard Gui, and set the adornee to the specific object using the function shown above and displaying it on the Gui.