Progress Bar acting weird

Issue

Hey, i was making a progress bar for a game and had a few errors.
Progress bar btw helps you know the position of yourself and other players on the x axis.
Here the problem that occurred from it.
image
As you can see its getting dragged about which makes it very weird.
when i say dragged. it means that the image is getting duplicated that it leaves a trail on every move

 local X2 = workspace.MainFolder:WaitForChild("Difficulty999"):WaitForChild("Part1").Position.X
local X1 = workspace.MainFolder:WaitForChild("Difficulty0").PrimaryPart.Position.X

local function GetPosition(X)
	return (X-X1)/(X2-X1)
end
local Color = Color3.fromRGB(85, 255, 0)

local function ColorConnection(p,v)
	if game.Players:GetPlayerFromCharacter(v.Parent)and game.Players:GetPlayerFromCharacter(v.Parent).UserId == game.Players.LocalPlayer.UserId and string.len(p.Name)>4 and tonumber(string.sub(p.Name,5))then
		Color = p.Color
	end
end
local function color1(p)
	if p:IsA("BasePart")then p.Touched:Connect(function(v)ColorConnection(p,v)end)end
end
local function color2(p)
	p.ChildAdded:Connect(color1)
end
workspace.MainFolder.ChildAdded:Connect(color2)
for i,v in pairs(workspace.MainFolder:GetChildren())do
	v.ChildAdded:Connect(color1)
	for i,v in pairs(v:GetChildren())do
		color1(v)
	end
end

script.Parent:WaitForChild("Main"):WaitForChild("Frame"):WaitForChild("Frame"):WaitForChild("ImageLabel").Image = game.Players:GetUserThumbnailAsync(game.Players.LocalPlayer.UserId,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size420x420)
while true do
	local A = GetPosition(game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position.X)
	if A > 1 then
		A = 1
	elseif A < 0 then
		A = 0
	end
	script.Parent:WaitForChild("Main"):WaitForChild("Frame"):WaitForChild("Frame").BackgroundColor3 = Color
	script.Parent.Main.Frame.Frame.Size = UDim2.fromOffset((script.Parent.Main.Frame.AbsoluteSize.X-33)*A+22,22)
	for i,v in pairs(game.Players:GetPlayers())do
		if v.Character then
		if v.UserId ~= game.Players.LocalPlayer.UserId then
			local img = script.Parent.Main.Frame.Players:FindFirstChild(v.Name)
			if not img then
				img = script.Parent.Main.Frame.ImageLabel:Clone()
				img.Name = v.Name
				img.Position = UDim2.fromOffset(A*GetPosition(v.Character:WaitForChild("HumanoidRootPart").Position.X)+11,0)
				img.Parent = script.Parent:WaitForChild("Main"):WaitForChild("Frame"):WaitForChild("Players")
				img.Image = game.Players:GetUserThumbnailAsync(v.UserId,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size420x420)
				img.Visible = true
			end
			local A = GetPosition(v.Character:WaitForChild("HumanoidRootPart").Position.X)
			if A > 1 then
				A = 1
			elseif A < 0 then
				A = 0
			end
			img.Position = UDim2.fromOffset((script.Parent.Main.Frame.AbsoluteSize.X-33)*A+11,0)
		end
		end
	end
	wait()
end 

Thanks for the help. Have a good day :four_leaf_clover:

There isn’t much information that you gave to help solve this problem, and a lot of people probably aren’t going to want to try to pick apart and understand your (pretty big) code sample, so some more information would be very helpful, as well as any debugging that you could do to find where in the code the problem happens.

I assume you want the icons on the bar to just not duplicate. If they are duplicating, you’re probably creating a new icon every time the position updates, instead of updating the old icon. I’d suggest creating the icon beforehand and only connecting the update to its position. Another way to do this would be checking if the icon exists: if it doesn’t, make one and change its position; if it does, just update the icon’s position.

So bacically its around this part of the code then

		if v.Character then
		if v.UserId ~= game.Players.LocalPlayer.UserId then
			local img = script.Parent.Main.Frame.Players:FindFirstChild(v.Name)
			if not img then
				img = script.Parent.Main.Frame.ImageLabel:Clone()
				img.Name = v.Name
				img.Position = UDim2.fromOffset(A*GetPosition(v.Character:WaitForChild("HumanoidRootPart").Position.X)+11,0)
				img.Parent = script.Parent:WaitForChild("Main"):WaitForChild("Frame"):WaitForChild("Players")
				img.Image = game.Players:GetUserThumbnailAsync(v.UserId,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size420x420)
				img.Visible = true
			end

I cloned the image here so thats prob the issue. However when i reference it it says im indexing nil or it couldnt find the label

That’s because your “img” variable is “local” in the scope (the area between the “then” and “end” of the statement) of the if statement on the second line in your code here, so anything that tries to access the “img” variable won’t be able to see it as it only exists in the scope of that if statement.

so i just put the img variable in the scope of the if statement>?

If you need to access it whenever the statement fires, it should be outside of the scope of whatever repeatedly needs to access it, so in this case you’d want to put it outside the “while true do” statement (which btw is bad practice, you should really link that to an event or some condition instead)

1 Like

woah that actually worked i think. im testing it right now. No errors at least tysm.

1 Like