I'm trying to make a graph that shows ping, it is working (all be it very slowly) but i'm having a problem getting rid of the segments I draw after they are off screen. Right now I have a script in each segment I draw that destroys them when they go farther then the background of the graph. When the bars get far enough that they would have been removed the game crashes.

As seen here

I have a suspicion it has to do with the fact I'm using AbsolutePosition because I've never really had a good experience with it in the past. I would use something else but I don't think there is a way. The graph boundaries scales with the users window size but the lines will always remain one pixel wide (as well as using offset instead of scale to move them), so using any other positioning method wouldn't account for that.

Here is the code contained in each segment:

function Move()
	if tonumber(script.Parent.AbsolutePosition.X) < tonumber(script.Parent.Parent.Parent.Back.AbsolutePosition.X) then
		script.Parent:Destroy()
	end
end

script.Parent.Changed:Connect(Move)

And here is the code I use to draw them, just in case you need it:

local TweenService = game:GetService("TweenService")

local NameList = {0}

while true do
	script.Parent.Back.Position = UDim2.new(0, script.Parent.Back.Position.X.Offset - 1, 0, 0)
	if script.Parent.Back.Position.X.Offset <= -40 then
		script.Parent.Back.Position = UDim2.new(0, 0, 0, 0)
	end
	local Plots = script.Parent.Plots:GetChildren()
	for i=1,#Plots do
		Plots[i].Position = UDim2.new(1, Plots[i].Position.X.Offset - 1, Plots[i].Position.Y.Scale, 0)
	end
	local CurrentPosition = Vector2.new(0, 1 - (script.Parent.Ping.Value / 200))
	
    local DataPoint = script.LineSegment:Clone()
    DataPoint.Position = UDim2.new(1, 0, 1 - (script.Parent.Ping.Value / 200), 0)
	DataPoint.Size = UDim2.new(0, 1, (script.Parent.Ping.Value / 200), 0)
    DataPoint.Parent = script.Parent.Plots
	DataPoint.BackgroundTransparency = 0
	local NameList = script.Parent.Plots:GetChildren()
	for i=1,#NameList do
		NameList[i] = tonumber(NameList[i].Name)
	end
	DataPoint.Name = math.max(unpack(NameList)) + 1
	DataPoint.BackgroundColor3 = Color3.fromHSV((120 - ((script.Parent.Ping.Value / 200) * 120)) / 359, 1, 1)
	if 120 - ((script.Parent.Ping.Value / 200) * 120) <= 0 then
		DataPoint.BackgroundColor3 = Color3.fromHSV(0, 1, 1)
	end
	
	wait(0.016)
end

I know that having a script in each segment is, to put it lightly, extremely inefficient. I will be changing it to a singular script, though I will still be using the same method so I will still have this problem.

Lastly any help on how to optimize the method of drawing the segments or destroying them would be greatly appreciated.

2 comments

0

Have you tried replacing 'while true do' with 'while wait(0.016) do' If you do that, you need to remove 'wait(0.016)' on line 31

0

@KBRM101 thanks, I'll do that but I dont think that'll fix the main problem at hand.