Button code is creating duplicate tables and not removing clone gui's from my game

Hi, I am new to this forum, I have 3 sets of code, which work together to take local player stats and paste them onto a gui to scroll through, unfortunately I have been struggling to make the local button script delete clone gui’s from the local player, and on top of that it has been dublicating the table created instead of clearing the table to then update later when the player opens the gui again, Can somebody help?

– local button script –

local RsService = game:GetService(“ReplicatedStorage”)

local event = RsService.Remote

local pgevent = RsService.ogremote

local TextButton = script.Parent

local PlayerGui = game:GetService(“Players”).LocalPlayer:WaitForChild(“PlayerGui”)

local ScreenGui = PlayerGui:FindFirstChild(“ScreenGui”)

local frame = ScreenGui:FindFirstChild(“ScrollingFrame”)

local Example = frame:FindFirstChild(“Example”)

local clickedalready = false

local playertimes = {}

local UserInputService = game:GetService(“UserInputService”)

TextButton.MouseButton1Click:Connect(function()

if clickedalready == false then

	print("open")

	pgevent:FireServer()


	local player = game.Players.LocalPlayer.Character

	if player then

		print("Button clicked by " .. player.Name)

	else

		print("Failed to find player")

	end


	event.OnClientEvent:Connect(function(playertable)

		playertimes = {} -- Clear the table before adding new values

		for i, innerTable in pairs(playertable) do

			local playerName = innerTable["player"]

			if playerName == "jaycool" then

				table.insert(playertimes, innerTable)

			end

		end
		print(playertimes)


		-- Create and populate the UI elements

		for i, times in pairs(playertimes) do

			local Newtime = Example:Clone()

			Newtime.Parent = frame

			Newtime.S1T.Text = times["S1"]

			Newtime.S2T.Text = times["S2"]

			Newtime.S3T.Text = times["S3"]

			Newtime.LT.Text = times["TT"]

		end


		script.Parent.Parent.ScrollingFrame.Visible = true

		clickedalready = true

	end)


else

	print("close")

	-- Clear the table and UI elements

	for i, times in pairs(playertimes) do

		table.remove(playertimes, i)

	end

	for i, child in pairs(frame:GetChildren()) do

		if child:IsA("GuiObject") and child ~= Example then

			child:Destroy()

		end

	end

	script.Parent.Parent.ScrollingFrame.Visible = false

	clickedalready = false

end

end)

– module script located in serverscriptservice–

local lapt = {}
lapt.laptimes = {}

local MT = {}

function lapt.new(player, s1, s2, s3)
local self = {}
setmetatable(self, MT)

self.player = player
self.S1 = s1
self.S2 = s2
self.S3 = s3
self.TT = s1 + s2 + s3

return self

end
return lapt

–script in serverscript service–

local Serverss = game:GetService(“ServerScriptService”)
local lap = require(Serverss.Lapse)
local laptimes = lap.laptimes
local sec1 = {}
local starttime = 0
local cool = 0
local example = lap.new(“jaycool”,4,3,2)
local example1 = lap.new(“nate”,1,1,1)
local example3 = lap.new(“jaycool”,5,8,2)
local example4 = lap.new(“nate”,1,1,1)
local example5 = lap.new(“nate”,1,1,1)
local example6 = lap.new(“jaycool”,7,3,1)
local example7 = lap.new(“jaycool”,7,4,1)
local example8 = lap.new(“jaycool”,7,4,3)
local example9 = lap.new(“jaycool”,7,4,4)
local examplea = lap.new(“jaycool”,7,5,4)
local exampleb = lap.new(“jaycool”,7,6,4)
local examplec = lap.new(“jaycool”,7,5,3)

local Cooldown = false
local lineB = 0

local partstart = game.workspace.checkpoints.start
local parts3 = game.workspace.checkpoints.s3

partstart.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) and not Cooldown then – Checks if the touch is a character model.
local player = game.Players:GetPlayerFromCharacter(hit.Parent) – Grabs player.
local humanoid = hit.Parent:FindFirstChild(“Humanoid”) – Finds the Humanoid object.

	lineB = 0

end

end)

parts3.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then – Checks if the touch is a character model.
local player = game.Players:GetPlayerFromCharacter(hit.Parent) – Grabs player.
local playerStatFolder = player:WaitForChild(“leaderstats”) – Identifies the player’s leaderstats.
local stat = playerStatFolder:WaitForChild(“LineBehavior”) – Identifies the player’s block stat.

	lineB = 1
end

end)

– lap times
table.insert(laptimes, example4)
table.insert(laptimes, example3)
table.insert(laptimes, example1)
table.insert(laptimes, example)
table.insert(laptimes, example5)
table.insert(laptimes, example6)
table.insert(laptimes, example7)
table.insert(laptimes, example8)
table.insert(laptimes, example9)
table.insert(laptimes, examplea)
table.insert(laptimes, exampleb)
table.insert(laptimes, examplec)

table.sort(sec1, function(a,b)
return a < b
end)

print(laptimes)

local event = game.ReplicatedStorage.Remote
local event2 = game.ReplicatedStorage.ogremote
wait(3)

event2.OnServerEvent:Connect(function(item)
event:FireAllClients(laptimes)
print(“work”)
end)

1 Like

the code is kinda muddled, sorry im new to this stuff