How to make a distance gui

If you’re familiar with Tower Of Hell, there is a height counter thing, and it shows who is the highest. I want to do this but with distance, how can this be accomplished. The only thing I don’t know how to do is calculate how far they are between the start and the end, and then put that onto a frame. Thanks in advance.

12 Likes

Sorry for being a little unclear, but instead of vertical the frame is horizontal.

7 Likes

This is quite simple, first get the y of the character position, then get the position of the end of the tower (the top), the divide the top of the tower positions Y, and the character positions Y. To get how far the player is up the tower.

A code example:

local char = (the character)
local topPos = Vector3.new() –– Position of the top of the tower

local dis = topPos.Y/char.PrimaryPart.Position.Y
print(dis) –– this will give you a percentage and then you can move the players icon on the tower gui relative to the percentage``` 
I made this on a phone, so I haven’t tested it out, and If you need more help just ask.
4 Likes

I meant distance, sorry if you didn’t understand that ( so left to right )

5 Likes

Is the tower vertical or horizontal?

5 Likes

Its not a Tower, and I am not making a tower of hell game. I was just referencing the bar on the side that tracks how far the player is.

Imagine there are two points, and I want to see how far the player is between those two points and put that position on a frame. (From left to right)

4 Likes

Oh, okay, in that case. What direction is the obby/thing you’re making going in, like x/y/z.

4 Likes

Ill make an example for you. It’ll take some time.

3 Likes

Is there a way to make it so that it doesn’t matter, I feel like its kind of irrelevant but for your sake we can say z

3 Likes

Okay, so just change my code from Y to Z. That’ll give a percentage then on your gui you can put that percentage for the thing that shows where the player is at. Like the position of the gui object.

3 Likes

How would I move the players image on the frame according to the dis variable

3 Likes

Udim2.new(), put the percentage on the x axis.

4 Likes

How would I scale that? Like this? :

playerThing.Position = UDim2.new(frame.AbsoluteSize.X/dis,frame.Y)
3 Likes

“playerThing.Position = UDim2.new(frame.AbsoluteSize.X/dis, 0, frame.Y, 0)”

3 Likes

It is saying that X is not a member of a part’s position?? Fixed: oops

3 Likes

Any clue to why this isn’t working? NO is printing every frame.

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

local frame = game.Players.LocalPlayer.PlayerGui:WaitForChild("DistanceGui").Frame

local function check(name)
	for _, v in pairs(game.Players:GetPlayers()) do
		if v.Name == name then continue end
		
		return true
	end
end

RunService.RenderStepped:Connect(function()
	for i, v in ipairs(game.Players:GetPlayers()) do
		local char = v.Character 
		local dis = game.Workspace.End.Position.X/char.PrimaryPart.Position.X
		
		if not frame:FindFirstChild(v.Name) then
			local userId = v.UserId
			local thumbType = Enum.ThumbnailType.HeadShot
			local thumbSize = Enum.ThumbnailSize.Size420x420
			local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)

			local imageLabel = Instance.new("ImageLabel")
			imageLabel.Parent = frame
			imageLabel.Image = content
			imageLabel.Size = UDim2.new(0, 420, 0, 420)
			imageLabel.Name = v.Name

			imageLabel.Position = UDim2.new(frame.AbsoluteSize.X/dis, 0, frame.AbsolutePosition.Y + 1, 0)
		end	
		
		for i, v in pairs(frame:GetChildren()) do
			if v:IsA("ImageLabel") then
				local bool = check(v.Name)
				if bool then
					print("NO")
					v:Destroy()
				else
					v.Position = UDim2.new(frame.AbsoluteSize.X/dis, 0, frame.AbsolutePosition.Y + 1, 0)
				end
			end
		end
	end
end)
2 Likes

Fixed the no thing, but now the icon just doesn’t come up at all.

2 Likes

How I would approach the problem is

Find distance is player (humanoidrootpart.position-part.position).Magnitude

1.local a = Find distance to part A
2. local b = Find distance to part B
3. local percent = a/b
4. the UI should accept a percent in either scale or offset forgot which

edit: sorry this is exzact thing the other helper said sorry didn’t read…

2 Likes

This doesn’t solve the problem of the icon not even showing up.

4 Likes

So I won’t help you out exactly how you want it, since I want you to learn, but I wrote a script that has some comments in it to explain why it works, but I want you to apply this to your own code without having to have your hand held throughout the whole thing. However if you do need help you can ask here too.

local rs = game:GetService("RunService")

local Distance = script.Parent.Distance
local Main = script.Parent.Main

local AD = Distance.AbsoluteSize
local AM = Main.AbsoluteSize

local p1 = workspace:WaitForChild("0")
local p2 = workspace:WaitForChild("1")

local pDist = (p2.Position-p1.Position).Magnitude -- Getting the distance between start and end goal

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local root = char:WaitForChild("HumanoidRootPart")


rs.RenderStepped:Connect(function()
	local plrDist = plr:DistanceFromCharacter(p1.Position) -- Getting the distance between player and start pos (this inverses the gui so that it moves fro left to right)
	local percentage = (plrDist/pDist) -- gets percentage the player is from the end position
	
	local XVal = math.clamp((AM.X + Main.AbsolutePosition.X) * (percentage),Main.AbsolutePosition.X,Main.AbsolutePosition.X + AM.X) -- clamps the UI's position so that it doesn't go out of bounds

	Distance.Position = UDim2.new(0,XVal,0,Main.AbsolutePosition.Y) -- setting positions
end)

All this script does is calculate the distance between the start and end, and the character and start. Then I divide the two together to get a percentage of sorts, it clamps the values from 0 - 1, which helps a ton when working with UI. I set the UI code in a specific way that it should work with my format but it would work with changes made to its positions and sizes. (ignore that rant if you don’t care) anywho, here’s it working, try to compare and contrast and if you can’t, ask later. Also this won’t work fully as intended, there are slight bugs, but I encourage you to engage with them and figure em out. Enjoy!

2 Likes