Length Bar for a race game

Hello, i want to make a length bar for my race game, something like Tower of Hell. It’s working while only one player is playing. But when 2 player plays at the same time, 2nd player to join the game can’t see first player’s avatars image on the length bar. And first player can see second one’s image but the image doesn’t move. Here’s the scripts

Script

local players = game.Players

game:GetService("Players").PlayerAdded:Connect(function(player)

	if player:FindFirstChild("PlayerGui") then
		while wait(0.5) do


			for _, plr in pairs(game.Players:GetChildren()) do

				local localimage = plr:WaitForChild("PlayerGui"):WaitForChild("Length"):WaitForChild("Frame"):WaitForChild(plr.Name.."Image")
				local localchar = game:GetService("Workspace")[plr.Character.Name]
				local plrhrp = localchar:FindFirstChild("HumanoidRootPart")
				local plrlength = math.ceil(plrhrp.Position.Z)
				local plrasdsad = plrlength - 20
				localimage.Position = UDim2.new(-0.035 + plrasdsad/16000, 0, 0.5, 0)
				localimage.ImageTransparency = 0
				print(plrlength)
				print(plrasdsad)
				print(plr.Name)

			end
		end
	end

end)

Script

local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size48x48
local players = game.Players

players.PlayerAdded:Connect(function(plr)
	wait(2)

	for _, plrr in pairs(game.Players:GetChildren()) do

		local cloneimage = 	plr.PlayerGui.Length.Frame.ImageLabel:Clone()
		cloneimage.Image = players:GetUserThumbnailAsync(plr.userId, thumbType, thumbSize)
		cloneimage.Parent = plrr.PlayerGui:WaitForChild("Length"):WaitForChild("Frame")
		cloneimage.Name = plr.Name.."Image"
		cloneimage.ImageTransparency = 0

	end


end)

Local Script

local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size48x48

local players = game.Players
local player = players.LocalPlayer

while wait(2) do
	player.PlayerGui.Length.Frame:WaitForChild(player.Name.."Image").Image = players:GetUserThumbnailAsync(player.userId, thumbType, thumbSize)
	player.PlayerGui.Length.Frame:WaitForChild(player.Name.."Image").ImageTransparency = 0
end

If these informations are not enough, you can ask anything you are wondering

1 Like

The two problems you are facing:

  1. When a player joins the game, they do not have other player’s images in their length bar
  2. When a player joins the game, the players that already were there can see the new players icon on their length bar, but the image does not move

The reason that new players do not get the images of the players that were already in the game is that the code that adds the images is only ran when a player joins, and when it runs it only adds the newest player.

That’s why new players only see their own, since when they join, their image gets added, but it doesn’t loop through and add all the players already in the game.

To fix this, all you got to do is when a player joins, the code that runs loops through all the players in the game, checks to see if that player has all the other player’s images, and if not, adds them.

Here is the second script you posted updated that should allow a player that joins to see everyone else’s image as well.

local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size48x48
local players = game.Players

players.PlayerAdded:Connect(function(plrJoined)
	local currentPlayers = game.Players:GetPlayers()
	for _, plr in ipairs(currentPlayers) do
		if plr ~= plrJoined then
			local frame = plr.PlayerGui:WaitForChild("Length"):WaitForChild("Frame")
			if not frame:FindFirstChild(plr.Name .. "Image") then
				local cloneimage = frame:WaitForChild("ImageLabel"):Clone()
				cloneimage.Image = players:GetUserThumbnailAsync(plrJoined.userId, thumbType, thumbSize)
				cloneimage.Parent = plr.PlayerGui:WaitForChild("Length"):WaitForChild("Frame")
				cloneimage.Name = plrJoined.Name.."Image"
				cloneimage.ImageTransparency = 0
			end
		else
			for _, plrAddImage in ipairs(currentPlayers) do
				local frame = plr.PlayerGui:WaitForChild("Length"):WaitForChild("Frame")
				if not frame:FindFirstChild(plrAddImage.Name .. "Image") then
					local cloneimage = frame:WaitForChild("ImageLabel"):Clone()
					cloneimage.Image = players:GetUserThumbnailAsync(plrAddImage.userId, thumbType, thumbSize)
					cloneimage.Parent = plrJoined.PlayerGui:WaitForChild("Length"):WaitForChild("Frame")
					cloneimage.Name = plrAddImage.Name.."Image"
					cloneimage.ImageTransparency = 0
				end
			end
		end
	end
end)

The problem where the new players images are not moving is a bit harder to diagnose. I think it has a similar issue as the problem above. Another thing I noticed is that the code to move the player images are within another .PlayerAdded connected function.

This means that every time a new player joins, this code will run again and again. Really it only needs to be ran once since it loops forever and checks each player within the game to move their image.

I would just extract the code within that .PlayerAdded connected function to be standalone. Here is the first script you posted updated:

local players = game.Players
while task.wait(0.5) do
	local currentPlayer = game.Players:GetPlayers()
	for _, moveplr in ipairs(currentPlayer) do
		for _, plr in ipairs(currentPlayer) do
			local playerGui = plr:FindFirstChild("PlayerGui")
			local lengthGui = playerGui and playerGui:FindFirstChild("Length")
			local frameGui = lengthGui and lengthGui:FindFirstChild("Frame")
			local localimage = frameGui and frameGui:FindFirstChild(moveplr.Name.."Image")

			local localchar = moveplr.Character
			local plrhrp = localchar and localchar:FindFirstChild("HumanoidRootPart")

			if localimage and plrhrp then
				local plrlength = math.ceil(plrhrp.Position.Z)
				local plrasdsad = plrlength - 20
				localimage.Position = UDim2.new(-0.035 + plrasdsad/16000, 0, 0.5, 0)
				localimage.ImageTransparency = 0
				print(plrlength)
				print(plrasdsad)
				print(plr.Name)
			end
		end
	end
end

Hopefully with those two scripts updated, players should receive other player’s images, as well as each image moving as they should.

I wrote the scripts quickly so there might be mistakes, let me know if you need any more help.

1 Like

Now it moves and second player can see first one’s image but first player cant see second one

I solved it

local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size48x48
local players = game.Players

players.PlayerAdded:Connect(function(plrJoined)
	local currentPlayers = game.Players:GetPlayers()
	for _, plr in ipairs(currentPlayers) do
		if plr ~= plrJoined then
			
			local frame = plr.PlayerGui:WaitForChild("Length"):WaitForChild("Frame")
			if not frame:FindFirstChild(plrJoined.Name .. "Image") then
				
				local cloneimage = frame:WaitForChild("ImageLabel"):Clone()
				cloneimage.Image = players:GetUserThumbnailAsync(plrJoined.userId, thumbType, thumbSize)
				cloneimage.Parent = plr.PlayerGui:WaitForChild("Length"):WaitForChild("Frame")
				cloneimage.Name = plrJoined.Name.."Image"
				cloneimage.ImageTransparency = 0
				
			end
			
		else
			
			for _, plrAddImage in ipairs(currentPlayers) do
				local frame = plr.PlayerGui:WaitForChild("Length"):WaitForChild("Frame")
				if not frame:FindFirstChild(plrAddImage.Name .. "Image") then
					
					local cloneimage = frame:WaitForChild("ImageLabel"):Clone()
					cloneimage.Image = players:GetUserThumbnailAsync(plrAddImage.userId, thumbType, thumbSize)
					cloneimage.Parent = plrJoined.PlayerGui:WaitForChild("Length"):WaitForChild("Frame")
					cloneimage.Name = plrAddImage.Name.."Image"
					cloneimage.ImageTransparency = 0
					
					local frame2 = game.StarterGui:WaitForChild("Length"):WaitForChild("Frame")
					local cloneimage2 = frame2:WaitForChild("ImageLabel"):Clone()
					cloneimage2.Image = players:GetUserThumbnailAsync(plrJoined.userId, thumbType, thumbSize)
					cloneimage2.Parent = game.StarterGui:WaitForChild("Length"):WaitForChild("Frame")
					cloneimage2.Name = plrJoined.Name.."Image"
					cloneimage2.ImageTransparency = 0
					
				end
			end
		end
		
	end
end)

I just changed this script:

			if not frame:FindFirstChild(plr.Name .. "Image") then

to this:

			if not frame:FindFirstChild(plrJoined.Name .. "Image") then

Thank you for helping

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.