Help get positions of parts in a UI

The title doesn’t give much context but I’m working on a radar like Interface which get’s the position of the parts and with some simple math their position is translated to a position which the user can see in the UI.


As seen in the UI there should be a part near me which there isn’t they’re far away from me put I think I did something wrong, the code will provide more insight.

	for i, target in Targets do
		if target:IsA("Part") then
			local Zcord = (Root.Position.Z - target.Position.Z) / 100
			local Xcord = (Root.Position.X - target.Position.X) / 100
			local NewPing = Ping:Clone()
			NewPing.Parent = Radar
			NewPing.Position = UDim2.new(Zcord, 0, Xcord, 0)
		end
	end

More experienced scripters may be baffled by the stupidity of this but math isn’t my strongest point, if there’s anything I could to position the pings correctly in the interface please inform me i’d be very thankful.

1 Like

The code is good in my opinion. Although the positions won’t get updated (unless you run the code snippet in a loop).

If you stand directly on top of a part, is its ping positioned in the middle of the radar?

1 Like

It is already running in a loop, the issue is the targets on the UI are in different positions than what they should be in.

1 Like

Ohh alright.

Hmm I think I see the problem.
Since the origin of a frame is at the top-left corner, the pings will appear shifted.

You should add 0.5 to the pings’ positions:
NewPing.Position = UDim2.new(Zcord + 0.5, 0, Xcord + 0.5, 0)

3 Likes

I don’t think that could quite fix it, I have set the anchor points of the frames to 0.5 on both X and Y positions, i could give it a try tho.

1 Like

Nevermind you were right setting the anchor point wasn’t enough, thank you so much for the help hope you have a nice day!

2 Likes

@6mikael6 should be correct. As an add-on, I would be a bit more specific and define your variables more thorough. If you think about the coordinate system in ROBLOX, the z-direction represents the depth, but when we transpose this into the two-dimensional radar, we should specify that they are converted to X and Y coordinates.
I personally think this helps with intuition, as well as working with the GUI later if you so choose!

local relativeX = (target.Position.X - Root.Position.X) / 100
local relativeY = (target.Position.Z - Root.Position.Z) / 100
1 Like

The Y cordinate doesn’t help much because I have the character has to go upwards to update which isn’t really helpful due to the fuck that player only goes within the X / Z cordinates.

The relativeX and relativeY variables take the inputs from the root and target X and Z variables respectively, but whenever you are transposing the NewPing on the radar, you are moving into a 2-dimensional system, which uses X and Y coordinates.
The variables I defined will not be looking at the y-position of the part or root, we are just calling the Z difference relativeY because the z-direction directly transposes to the y-direction on the radar.