Height Bar GUI show all players

I’ve managed to make a Height Bar GUI to visualize how far a player is up a set of stairs, I’ve looked everywhere and couldn’t find how to add all players in the server on the bar. If anyone has experience, help would be much appreciated!

image

HeightScript:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local PlayerGui = Player:FindFirstChild("PlayerGui")

if Player:FindFirstChild("PlayerGui") then
    while wait() do
        local ImageLabel = PlayerGui.HeightGui.Frame:FindFirstChild("ImageLabel")
        local Character = game:GetService("Workspace")[Player.Name]
        local PrimaryPart = Character:FindFirstChild("HumanoidRootPart")
		local Height = math.ceil(PrimaryPart.Position.Y)
		local totalrange = -765.125
		local percent = Height / totalrange
		ImageLabel.Position = UDim2.new(-0.25, 0, percent + 1, 0)
    end
end

Image Script:

local ThumbnailType = Enum.ThumbnailType.HeadShot
local ThumbnailSize = Enum.ThumbnailSize.Size48x48
local Players = game:GetService("Players")
local Player = Players.LocalPlayer

while wait() do
    script.Parent.Image = Players:GetUserThumbnailAsync(Player.userId, ThumbnailType, ThumbnailSize)
end

In Game:
image

(note: bottom of bar huminodrootpart position y is “0” and top of the bar is “765.125”)

1 Like

Couple of things.

You should only have one script, and you should not be setting the player’s image in a while wait() loop. You also shouldn’t be using while wait() to update the player’s position on the frame. A way to add all players is by using a loop.

Replace both LocalScripts with one (that is under Frame and not under ImageLabel) that has this pasted inside:

--//Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Frame = script.Parent
local ImageLabel = script.ImageLabel

--//Controls
local ThumbnailType = Enum.ThumbnailType.HeadShot
local ThumbnailSize = Enum.ThumbnailSize.Size48x48

--//Functions
local function OnPlayerAdded(player)
	local newImage = ImageLabel:Clone()
	newImage.Name = player.UserId
	newImage.Image = Players:GetUserThumbnailAsync(player.UserId, ThumbnailType, ThumbnailSize)
	newImage.Parent = Frame
	
	local HumanoidRootPart = nil
	
	player.CharacterAdded:Connect(function(character)
		HumanoidRootPart = character:WaitForChild("HumanoidRootPart", 5)
		
		local updateConnection = nil
		
		updateConnection = RunService.Heartbeat:Connect(function()
			if not HumanoidRootPart then
				updateConnection:Disconnect()
				
				return
			end
			
			local currentHeight = math.ceil(HumanoidRootPart.Position.Y)
			local highestPoint = -765.125
			local percent = currentHeight / highestPoint
			ImageLabel.Position = UDim2.fromScale(-0.25, percent + 1)
		end)
	end)
	
	player.CharacterRemoving:Connect(function()
		HumanoidRootPart = nil
	end)
end

local function OnPlayerRemoving(player)
	local currentImage = Frame:FindFirstChild(player.UserId)
	
	if currentImage then
		currentImage:Destroy()
	end
end

Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)

for i, player in ipairs(Players:GetPlayers()) do
	task.spawn(OnPlayerAdded, player)
end

Layout of the script if needed:
image

3 Likes

Thanks for the quick reply! after replacing the new script with the old and adjusting the layout. It seems to only update the image, Not the actual position of the image. Maybe I made a mistake on my end that I didn’t see?

image

1 Like

I forgot to account for if the player’s character already exists. Your current setup looks right.

Code:

--//Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Frame = script.Parent
local ImageLabel = script.ImageLabel

--//Controls
local ThumbnailType = Enum.ThumbnailType.HeadShot
local ThumbnailSize = Enum.ThumbnailSize.Size48x48

--//Functions
local function OnPlayerAdded(player)
	local newImage = ImageLabel:Clone()
	newImage.Name = player.UserId
	newImage.Image = Players:GetUserThumbnailAsync(player.UserId, ThumbnailType, ThumbnailSize)
	newImage.Parent = Frame

	local HumanoidRootPart = nil
	
	local function OnCharacterAdded(character)
		HumanoidRootPart = character:WaitForChild("HumanoidRootPart", 5)

		local updateConnection = nil

		updateConnection = RunService.Heartbeat:Connect(function()
			if not HumanoidRootPart then
				updateConnection:Disconnect()

				return
			end

			local currentHeight = math.ceil(HumanoidRootPart.Position.Y)
			local highestPoint = -765.125
			local percent = currentHeight / highestPoint
			ImageLabel.Position = UDim2.fromScale(-0.25, percent + 1)
		end)
	end

	player.CharacterAdded:Connect(OnCharacterAdded)

	player.CharacterRemoving:Connect(function()
		HumanoidRootPart = nil
	end)
	
	if player.Character then
		OnCharacterAdded(player.Character)
	end
end

local function OnPlayerRemoving(player)
	local currentImage = Frame:FindFirstChild(player.UserId)

	if currentImage then
		currentImage:Destroy()
	end
end

Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)

for i, player in ipairs(Players:GetPlayers()) do
	task.spawn(OnPlayerAdded, player)
end
1 Like

Hmm still not working, Could there be something else?
image

1 Like

See if it works after you reset.

Code:

--//Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Frame = script.Parent
local ImageLabel = script.ImageLabel

--//Controls
local ThumbnailType = Enum.ThumbnailType.HeadShot
local ThumbnailSize = Enum.ThumbnailSize.Size48x48

--//Functions
local function OnPlayerAdded(player)
	local newImage = ImageLabel:Clone()
	newImage.Name = player.UserId
	newImage.Image = Players:GetUserThumbnailAsync(player.UserId, ThumbnailType, ThumbnailSize)
	newImage.Parent = Frame
	
	local Character = player.Character or player.CharacterAdded:Wait()
	local HumanoidRootPart = nil
	
	local function OnCharacterAdded(character)
		HumanoidRootPart = character:WaitForChild("HumanoidRootPart", 5)

		local updateConnection = nil

		updateConnection = RunService.Heartbeat:Connect(function()
			if not HumanoidRootPart then
				updateConnection:Disconnect()

				return
			end

			local currentHeight = math.ceil(HumanoidRootPart.Position.Y)
			local highestPoint = -765.125
			local percent = currentHeight / highestPoint
			ImageLabel.Position = UDim2.fromScale(-0.25, percent + 1)
		end)
	end

	player.CharacterAdded:Connect(OnCharacterAdded)

	player.CharacterRemoving:Connect(function()
		HumanoidRootPart = nil
	end)
	
	OnCharacterAdded(Character)
end

local function OnPlayerRemoving(player)
	local currentImage = Frame:FindFirstChild(player.UserId)

	if currentImage then
		currentImage:Destroy()
	end
end

Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)

for i, player in ipairs(Players:GetPlayers()) do
	task.spawn(OnPlayerAdded, player)
end
1 Like

Very weird, still not working. Based on the beginner-intermediate coding knowledge I have, the code you provided looks like it should work. I really don’t know why it doesn’t???

1 Like

Could you add a print inside the OnCharacterAdded function and tell me if it prints anything? Apologies for the inconvenience.

1 Like

Sure man,

Made 2 prints at the start and end of the OnCharacterAdded function

--//Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Frame = script.Parent
local ImageLabel = script.ImageLabel

--//Controls
local ThumbnailType = Enum.ThumbnailType.HeadShot
local ThumbnailSize = Enum.ThumbnailSize.Size48x48

--//Functions
local function OnPlayerAdded(player)
	print("before")
	local newImage = ImageLabel:Clone()
	newImage.Name = player.UserId
	newImage.Image = Players:GetUserThumbnailAsync(player.UserId, ThumbnailType, ThumbnailSize)
	newImage.Parent = Frame

	local Character = player.Character or player.CharacterAdded:Wait()
	local HumanoidRootPart = nil

	local function OnCharacterAdded(character)
		HumanoidRootPart = character:WaitForChild("HumanoidRootPart", 5)

		local updateConnection = nil

		updateConnection = RunService.Heartbeat:Connect(function()
			if not HumanoidRootPart then
				updateConnection:Disconnect()

				return
			end

			local currentHeight = math.ceil(HumanoidRootPart.Position.Y)
			local highestPoint = -765.125
			local percent = currentHeight / highestPoint
			ImageLabel.Position = UDim2.fromScale(-0.25, percent + 1)
		end)
		print("after")
	end

	player.CharacterAdded:Connect(OnCharacterAdded)

	player.CharacterRemoving:Connect(function()
		HumanoidRootPart = nil
	end)

	OnCharacterAdded(Character)
end

local function OnPlayerRemoving(player)
	local currentImage = Frame:FindFirstChild(player.UserId)

	if currentImage then
		currentImage:Destroy()
	end
end

Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)

for i, player in ipairs(Players:GetPlayers()) do
	task.spawn(OnPlayerAdded, player)
end

Seemed to both print:
image

Thanks for the help you’ve been providing me I really appreciated it! :slight_smile:

1 Like

Could you try to print the HumanoidRootPart? I have a feeling it isn’t loading in quickly enough.

1 Like

Here?
image

1 Like

Yes, but I meant actually printing the HumanoidRootPart.

Like this:

print(HumanoidRootPart)
2 Likes

Got it.

Printed but took a sec:
image

Now, could you move that print inside the .Heartbeat function? Everything seems like it should work right now so it’s confusing me a bit.

1 Like

The HumanoidRootPart seems to be loading properly, I’m absolutely puzzled.
image

Try changing your position line to this:

ImageLabel.Position = UDim2.new(-0.25, 0, percent + 1, 0)

I changed it earlier to use UDim2.fromScale but that might have caused errors if your UI object starts off with an offset that isn’t 0.

If you could print the other variables in that loop, that would help too.

1 Like

Already tried that unfortunately, and didnt work. (will try one more time to make sure)
edit: still doesn’t

1 Like

Would it be possible to add you to a place with the gui in it so we don’t have to keep going back and forth? If not I don’t mind doing what you say but just would take a bit longer

Sure, you can add me to the place.

1 Like