Need help with resolving extreme memory leaks for endurance racing league

Hi, I own a roblox racing league called REC. We host long endurance races that last upwards of 2 hours to 4 hours, and I’ve noticed a very important issue that needs to be resolved that I cannot figure out. During the race, the total kb/s went up to 9gb per second, and never went down. The rate/s for the main script also went up - to almost 448,000 - and kept rising exponentially.

picture of the kb/s at one point in the race
Screenshot 2026-01-03 104741

Some quick background on how the systems work:
There is a main script in the workspace called Systems, which loads the other modules that control timing systems, pitstops, etc. The heirarchy looks like this:


(The Scripts folder has been ruled out as a source of the lag)

The Systems script looks like this:

print("[Systems] Loading systems...")

local Data = require(script.Data)
Data:Load()

-- Load Modules with tracking
for _, Module in script.Modules:GetChildren() do
	local s, m = pcall(function()
		local mod = require(Module)
		mod:Load()
	end)

	if not s then
		warn(`Error loading module {Module.Name}: {m}`)
	end
end

-- Load Static with tracking
for _, Module in script.Static:GetChildren() do
	local s, m = pcall(function()
		local mod = require(Module)
		mod:Load()
	end)

	if not s then
		warn(`Error loading module {Module.Name}: {m}`)
	end
end

The rate for this script rose to over 400,000/s and never stopped rising.

The timing systems involve position roughly 150 parts around the track, that when a player touches them, it stores this in the user’s CarData. All car data is controlled in one module script, Data, whose structure looks like this:

local Data = {
	Cars = {},
	
	Authorized = {
		1382359571,
		-1,
		-2
	},
	
	CarTemplate = {
		TeamName = nil,
		CarClass = nil,
		CarNumber = nil,
		CarName = nil,
		
		CarModel = nil,
		
		Driving = nil,
		SessionFinished = false,
		
		Members = {},
		AvailableMembers = {},
	},
}

local Utils = require(workspace.Systems.Utils)
local RunService = game:GetService("RunService")

function Data:GetCar(CarNumber)
	for _, Car in Data.Cars do
		if Car.CarNumber == CarNumber then
			return Car
		end
	end
	
	return nil
end

function Data:CreateCar(TeamName, CarClass, CarNumber, CarName, AvailableMembers, LiveryID)
	local CarData = Data:GetCar(CarNumber)
	
	if CarData then
		return CarData
	end
	
	local NewCarData = Utils:DeepClone(Data.CarTemplate)
	NewCarData.TeamName = TeamName
	NewCarData.CarClass = CarClass
	NewCarData.CarNumber = CarNumber
	NewCarData.CarName = CarName
	NewCarData.AvailableMembers = AvailableMembers
	NewCarData.Members = {}
	NewCarData.LiveryID = LiveryID
	
	for _, authorizedMember in Data.Authorized do
		table.insert(NewCarData.AvailableMembers, authorizedMember)
	end
	
	table.insert(Data.Cars, NewCarData)
	
	for _, Module in workspace.Systems.Modules:GetChildren() do
		if not Module:IsA("ModuleScript") then
			continue
		end
		
		Module = require(Module)
		Module:RegisterCar(CarNumber)
	end
	
	return NewCarData
end

function Data:GetPlayerCar(UserID)
	for _, Car in Data.Cars do
		if table.find(Car.Members, UserID) then
			return Car
		end
	end
	
	if workspace.Systems.Config.PopulateTeams.Value then
		local NewCar = Data:CreateCar(game.Players:GetPlayerByUserId(UserID).Name, "GTP", #Data.Cars + 101, "any_car", {UserID})
		NewCar.Members = {UserID}
		return NewCar
	end
	
	return nil
end

function Data:TriggerDataSend()
	script.CarsUpdate:FireAllClients(Data.Cars)
end

function Data:Load()
	game.Players.PlayerAdded:Connect(function(Player)
		Data:GetPlayerCar(Player.UserId)
	end)
	
	script.UpdateMember.OnServerEvent:Connect(function(player, desiredCarNumber)
		print(`{player.Name} requesting to swap to car #{desiredCarNumber}`)
		local newCarData = Data:GetCar(desiredCarNumber)
		if not newCarData then return end
		
		if table.find(newCarData.Members, player.UserId) then print("Already registered to car") return end
		if not table.find(newCarData.AvailableMembers, player.UserId) then print("Can't register to car because player is not in AvailableMembers") return end
		
		for _, carData in Data.Cars do
			if table.find(carData.Members, player.UserId) then
				table.remove(carData.Members, table.find(carData.Members, player.UserId))
			end
		end
		
		print(`{player.Name} swapped to car #{desiredCarNumber}`)
		table.insert(newCarData.Members, player.UserId)
	end)
	
	script.UpdateClass.OnServerEvent:Connect(function(Player, NewClass)
		local CarData = Data:GetPlayerCar(Player.UserId)
		if not CarData then return end
		
		CarData.CarClass = NewClass
	end)
	
	task.spawn(function()
		while task.wait(0.25) do
			for _, Car in Data.Cars do
				if (not Car.CarModel) or (not Car.CarModel.Parent) then
					Car.Driving = nil
					Car.CarModel = nil
					continue 
				end
				
				local DriveSeat = Car.CarModel:FindFirstChild("DriveSeat", true)
				local UserID = DriveSeat.Occupant and game.Players:GetPlayerFromCharacter(DriveSeat.Occupant.Parent).UserId or nil
				
				Car.Driving = UserID
			end
			
			Data:TriggerDataSend()
		end
	end)
	
	task.spawn(function()
		while task.wait(3) do
			local MembersUsed = {}
			for _, Car in Data.Cars do
				if not Car.Members then continue end
				
				for _, UserID in Car.Members do
					if table.find(MembersUsed, UserID) then
						table.remove(Car.Members, table.find(Car.Members, UserID))
						warn("Member was found in 2 cars.")
						continue
					end
					
					table.insert(MembersUsed, UserID)
				end
			end
		end
	end)
	
	Data:RegisterCars()
end

function Data:RegisterCars()
	Data:CreateCar("VOLT Racing", "GTD", 992, "porsche_gtd", {584119376, 1683131739, 984058467, 325142584, 186479076, 1287142992, 1142170345}, 111942964999544)
	Data:CreateCar("VOLT Racing", "GTP", 5, "porsche_gtp", {584119376, 1683131739, 984058467, 325142584, 186479076, 1287142992, 1142170345}, 77701542124977)
	Data:CreateCar("VOLT Racing", "GTD", 911, "porsche_gtd", {584119376, 1683131739, 984058467, 325142584, 186479076, 1287142992, 1142170345}, 113938706047160)
	Data:CreateCar("Redwing Racing", "GTP", 28, "porsche_gtp", {2960441901, 193284491, 158614472, 8804671506, 1321754272, 480052787, 1126605473, 1512610646, 3815419577, 1719677683}, 96959237812837)
	Data:CreateCar("Redwing Racing", "GTD", 39, "corvette_gtd", {2960441901, 193284491, 158614472, 8804671506, 1321754272, 480052787, 1126605473, 1512610646, 3815419577, 1719677683}, 79928425126418)
	Data:CreateCar("Redwing Racing", "GTD", 26, "corvette_gtd", {2960441901, 193284491, 158614472, 8804671506, 1321754272, 480052787, 1126605473, 1512610646, 3815419577, 1719677683}, 109045425052444)
	Data:CreateCar("FIAR CORSE", "GTD", 8, "bmw_gtd", {3503338689, 4552293109, 3590940097, 3022433158}, 125284851034451)
	Data:CreateCar("Panther TCR", "GTD", 73, "mclaren_gtd", {274172168, 1818629397, 161170235, 1786929776})
	Data:CreateCar("Zan Leoj Racing", "GTP", 37, "cadillac_gtp", {1506536977, 1143592086, 461390153, 2233296494}, 73880401294528)
	Data:CreateCar("Zan Leoj Racing", "GTD", 52, "mclaren_gtd", {1506536977, 1143592086, 461390153, 2233296494})
	Data:CreateCar("Watermelon Racing", "GTP", 77, "acura_gtp", {1377711166, 1215454634, 1567901986}, 100193314448606)
	Data:CreateCar("Latos Racing", "GTP", 82, "porsche_gtp", {775869683, 485091221, 8144966349}, 129227122832681)
	Data:CreateCar("Bita Racing", "GTP", 7, "bmw_gtp", {3886163014, 7256311710, 474667979, 1167631121, 1614916812}, 88805201724542)
	Data:CreateCar("Bita Racing", "GTD", 89, "corvette_gtd", {3886163014, 7256311710, 474667979, 1167631121, 1614916812}, 96953084673906)
	Data:CreateCar("Alpha Performance ", "GTD", 17, "porsche_gtd", {1880476077, 7818733726, 192199976}, 139951743141195)
	Data:CreateCar("Irish Motorsports", "GTD", 4, "bmw_gtd", {2792424500, 136560166, 210553335, 1070141606, 132990966}, 131871132581188)
	Data:CreateCar("Irish Motorsports", "GTD", 44, "bmw_gtd", {2792424500, 136560166, 210553335, 1070141606, 132990966}, 128862232499446)
	Data:CreateCar("Irish Motorsports", "GTP", 45, "bmw_gtp", {2792424500, 136560166, 210553335, 1070141606, 132990966})
	Data:CreateCar("Hue Performance ", "GTD", 10, "porsche_gtd", {2598847338, 1343875424, 7807095767, 634762045, 4695977787, 1521089777, 3159725977, 403407809}, 109605368074355)
	Data:CreateCar("Hue Performance ", "GTP", 34, "porsche_gtp", {2598847338, 1343875424, 7807095767, 634762045, 4695977787, 1521089777, 3159725977, 403407809}, 134982098393043)
	Data:CreateCar("Azerbaijan Technology", "GTD", 999, "bmw_gtd", {2377421095, 5695186421})
	Data:CreateCar("Spectron Racing Team", "GTP", 2, "cadillac_gtp", {74779517, 52804085, 1527617484, 8637437797, 550840111})
	Data:CreateCar("Spectron Racing Team", "GTD", 3, "mustang_gtd", {74779517, 52804085, 1527617484, 8637437797, 550840111})
	Data:CreateCar("Celeritas Racing", "GTD", 29, "porsche_gtd", {706960268, 2976372807}, 80277621100427)
	Data:CreateCar("Celeritas Racing", "GTP", 92, "cadillac_gtp", {706960268, 2976372807})
	Data:CreateCar("Okimura Motorsports", "GTD", 222, "mustang_gtd", {1896907489})
	Data:CreateCar("Okimura Motorsports", "GTD", 42, "mustang_gtd", {1896907489})
	Data:CreateCar("GranTan Racing", "GTD", 25, "bmw_gtd", {2728282908}, 112458425761972)
	Data:CreateCar("GranTan Racing", "GTD", 33, "mustang_gtd", {2728282908}, 95684445352675) 
end

return Data

For reference - the PopulateTeams was turned off, so only the 29 cars were ever created

Then, each module will store it’s data here.
An important note that the Timing module also contains over 300 parts to detect when a player cuts the track

I’m aware that obviously a rate of 450,000+ indicates a memory leak, but I cant think as to where this is coming from. I also cannot find a way to diagnose each individual module, only the main script that calls require() on them.

This is the Timing script:

local Timing = {
	CarTemplate = {
		LapInvalid = false,
		LastLapInvalid = false,
		LastCornerCutAt = 0,
		
		LastSectorTouched = nil,
		SectorTouchTimes = {},
		SectorTimes = {},
		
		LastLapTime = nil,
		FastestLapTime = nil,
		LapCount = 0,
	}
}

local Utils = require(workspace.Systems.Utils)
local Data = require(workspace.Systems.Data)
local Ordering = require(workspace.Systems.Modules.Ordering)
local RaceControl = require(workspace.Systems.Static.RaceControl)

function Timing:RegisterCar(CarNumber)
	local CarData = Data:GetCar(CarNumber)
	CarData.Timing = Utils:DeepClone(Timing.CarTemplate)
end

function Timing:InvalidateLap(CarNumber)
	local CarData = Data:GetCar(CarNumber)
	
	if not CarData then
		return
	end
	
	CarData.Timing.LapInvalid = true
end

function CCLogic(CarNumber)
	local CarData = Data:GetCar(CarNumber)
	
	if not CarData then
		return
	end
	
	if os.clock() - CarData.Timing.LastCornerCutAt < 3 then
		return
	end
	
	CarData.Timing.LastCornerCutAt = os.clock()
	CarData.Penalties.CornerCuts += 1
	Timing:InvalidateLap(CarNumber)
	
	for _, userID in CarData.Members do
		local player = game.Players:GetPlayerByUserId(userID)
		if not player then continue end
		
		script.Events.LapInvalid:FireClient(player)
	end
end

function SectorLogic(CarNumber, Sector)
	local TIME_TOUCHED = math.round(os.clock() * 1000) / 1000
	
	local CarData = Data:GetCar(CarNumber)
	if not CarData then return end
	
	if CarData.SessionFinished then
		return
	end
	
	local LastSector = CarData.Timing.LastSectorTouched
	if LastSector == Sector then return end
	
	local ExpectedPreviousSector = (Sector == 1) and 3 or Sector - 1
	if LastSector ~= ExpectedPreviousSector then
		Timing:InvalidateLap(CarNumber)
	end
	
	if Sector == 3 then
		local LastTouchTime = CarData.Timing.SectorTouchTimes[3]
		
		if LastTouchTime then
			local LapTime = math.round((TIME_TOUCHED - LastTouchTime) * 1000) / 1000
			CarData.Timing.LastLapTime = LapTime
			
			if not CarData.Timing.LapInvalid then
				local FastestLapTime = CarData.Timing.FastestLapTime
				
				if FastestLapTime and LapTime < FastestLapTime then
					CarData.Timing.FastestLapTime = LapTime
				end
				
				if not FastestLapTime then
					CarData.Timing.FastestLapTime = LapTime
				end
			end
			
			script.Events.LapSet:FireAllClients(CarNumber, LapTime)
		end
		
		CarData.TrackTiming.LastTimingPart = 0
		CarData.Timing.LapCount += 1
		CarData.Timing.LastLapInvalid = CarData.Timing.LapInvalid
		CarData.Timing.LapInvalid = false
	end
	
	if Sector == 1 then
		CarData.Timing.SectorTouchTimes = {
			[3] = CarData.Timing.SectorTouchTimes[3]
		}
		
		CarData.Timing.SectorTimes = {}
	end
	
	CarData.Timing.SectorTouchTimes[Sector] = TIME_TOUCHED
	if CarData.Timing.SectorTouchTimes[ExpectedPreviousSector] then
		CarData.Timing.SectorTimes[Sector] = math.round((TIME_TOUCHED - CarData.Timing.SectorTouchTimes[ExpectedPreviousSector]) * 1000) / 1000
	end
	
	CarData.Timing.LastSectorTouched = Sector
	
	------------------------------------------------------
	
	if Sector ~= 3 then
		return
	end
	
	local LeaderFinished = false
	if Ordering.Leader and Ordering.Leader.SessionFinished then
		LeaderFinished = true
	end
	
	local EndsAt = RaceControl.SessionEnd
	local TimeLeftInSession = EndsAt - workspace:GetServerTimeNow()
	
	if TimeLeftInSession <= 0  then
		if RaceControl.OrderingMode == RaceControl.OrderingModes.TRACK_POSITION 
			and (LeaderFinished or Ordering.Leader.CarNumber == CarNumber) then
			
			CarData.SessionFinished = true
		elseif RaceControl.OrderingMode == RaceControl.OrderingModes.BEST_LAP then
			CarData.SessionFinished = true
		end
	end
end

function RegisterSectorPart(Part, SectorNumber)
	Part.Touched:Connect(function(OtherObject: Part)
		local CarModel = OtherObject:FindFirstAncestor("A-Chassis")
		if not CarModel then return end

		local Occupant = CarModel:FindFirstChild("DriveSeat", true).Occupant
		if not Occupant then return end
		local Player = game.Players:GetPlayerFromCharacter(Occupant.Parent)
		local Car = Data:GetPlayerCar(Player and Player.UserId or 0)
		if not Car then return end
		local CarNumber =  Car.CarNumber
		
		SectorLogic(CarNumber, SectorNumber)
	end)
end

function RegisterCCPart(Part)
	Part.Touched:Connect(function(OtherObject: Part)
		local CarModel = OtherObject:FindFirstAncestor("A-Chassis")
		if not CarModel then return end
		
		local Occupant = CarModel:FindFirstChild("DriveSeat", true).Occupant
		if not Occupant then return end
		
		local Player = game.Players:GetPlayerFromCharacter(Occupant.Parent)
		local Car = Data:GetPlayerCar(Player and Player.UserId or 0)
		if not Car then return end
		local CarNumber =  Car.CarNumber
		
		CCLogic(CarNumber)
	end)
end

function Timing:Load()
	RegisterSectorPart(script.Parts.S1, 1)
	RegisterSectorPart(script.Parts.S2, 2)
	RegisterSectorPart(script.Parts.S3, 3)
	
	for _, Part in script.Parts.CC:GetChildren() do
		RegisterCCPart(Part)
	end
end

return Timing

And this is the TrackTiming script:

local TrackTiming = {
	CarTemplate = {
		LastTimingPart = 0,
		TimingPartTimes = {},
		TotalParts = 0,
		
		BestTimes = {},
		Delta = 0,
	},
	
	TotalParts = 0,
}

local Data = require(workspace.Systems.Data)
local Utils = require(workspace.Systems.Utils)

function TrackTiming:RegisterCar(carNumber)
	local carData = Data:GetCar(carNumber)
	carData.TrackTiming = Utils:DeepClone(TrackTiming.CarTemplate)
end

function TrackTimingLogic(carNumber, partNumber)
	local TIME_TOUCHED = math.round(os.clock() * 1000) / 1000
	
	local carData = Data:GetCar(carNumber)
	if not carData then return end
	
	if carData.SessionFinished then return end
	if carData.TrackTiming.LastTimingPart == partNumber then return end
	
	local carDataTrackTiming = carData.TrackTiming
	local carDataTiming = carData.Timing
	
	if partNumber == 1 then
		if carDataTiming.LastLapTime == carDataTiming.FastestLapTime then
			carDataTrackTiming.BestTimes = Utils:DeepClone(carDataTrackTiming.TimingPartTimes)
		end
	end
	
	carDataTrackTiming.LastTimingPart = partNumber
	carDataTrackTiming.TimingPartTimes[partNumber] = TIME_TOUCHED
	
	----------- IMPORTANT FOR ORDERING
	carDataTrackTiming.TotalParts = (carDataTiming.LapCount * TrackTiming.TotalParts) + partNumber
	
	if carDataTrackTiming.BestTimes[1] and carDataTrackTiming.BestTimes[partNumber] then
		local bestDiff = carDataTrackTiming.BestTimes[partNumber] - carDataTrackTiming.BestTimes[1]
		local currentDiff = carDataTrackTiming.TimingPartTimes[partNumber] - carDataTrackTiming.TimingPartTimes[1]
		
		carDataTrackTiming.Delta = currentDiff - bestDiff
	end
end

function RegisterPart(part, partNumber)
	part.Touched:Connect(function(otherObject: Part)
		local carModel = otherObject:FindFirstAncestor("A-Chassis")
		if not carModel then return end

		local occupant = carModel:FindFirstChild("DriveSeat", true).Occupant
		if not occupant then return end
		
		local player = game.Players:GetPlayerFromCharacter(occupant.Parent)
		local car = Data:GetPlayerCar(player and player.UserId or 0)
		if not car then return end
		local carumber =  car.CarNumber
		TrackTimingLogic(carumber, partNumber)
	end)
end

function TrackTiming:Load()
	for _, part in script.Parts:GetChildren() do
		local partNumber = tonumber(part.Name)
		RegisterPart(part, partNumber)
	end
	
	TrackTiming.TotalParts = #script.Parts:GetChildren()
end

return TrackTiming

Any help would be greatly appreciated, as to how to go about diagnosing specifically which module is causing the extreme lag as well as if there is any specific code that i’ve sent that could cause a memory leak as large as the one that i’ve described.

I also have hte microprofiler logs from during the race where the kb/s and r/s were very high, if those are of any help.

1 Like

Have you tried looking at LuauHeap?

I have, but unfortunately I did not look at it during the race when the lag occured. I have been having an extremely hard time replicating the memory leak in studio - it’s perfectly fine in studio. But I know for sure its the systems causing the memory leak - just not which module or what specifically from it.

I’ve found it, however it doesn’t say precisely which module it came from which is making the fixing of the memory leak extremely hard. This is a picture of it:


image

(Snapshots are about a minute apart)

The rate/s represents how many times per second that script is being fired. If it is required, fired or invoked by other scripts, then check every single reference and make sure that all connections are disconnected after use (which is what it suggests to me).

Checking the rate/s on other scripts might indicate where the problem is originating from. Your kb/s seems more likely a symptom of the data being updated with almost exponential frequency.

I find that very interesting. For some reason I am unable to create the exponential rise of rate/s in studio, and even when creating a local test server with 8 players driving around, I am unable to re-create it.

And while i’m not sure of its importantance, this particular script here was allocating large amounts of memory as seen in the LuauHeap, and it never gets garbage collected. It simply rises every time a driver touches one of the TrackTimingParts

local TrackTiming = {
	CarTemplate = {
		LastTimingPart = 0,
		TimingPartTimes = {},
		TotalParts = 0,
		
		BestTimes = {},
		Delta = 0,
	},
	
	TotalParts = 0,
}

local Data = require(workspace.Systems.Data)
local Utils = require(workspace.Systems.Utils)

function TrackTiming:RegisterCar(carNumber)
	local carData = Data:GetCar(carNumber)
	carData.TrackTiming = Utils:DeepClone(TrackTiming.CarTemplate)
end

function TrackTimingLogic(carNumber, partNumber)
	local TIME_TOUCHED = math.round(os.clock() * 1000) / 1000
	
	local carData = Data:GetCar(carNumber)
	if not carData then return end
	
	if carData.SessionFinished then return end
	if carData.TrackTiming.LastTimingPart == partNumber then return end
	
	local carDataTrackTiming = carData.TrackTiming
	local carDataTiming = carData.Timing
	
	if partNumber == 1 then
		if carDataTiming.LastLapTime == carDataTiming.FastestLapTime then
			carDataTrackTiming.BestTimes = table.clone(carDataTrackTiming.TimingPartTimes)
		end
	end
	
	carDataTrackTiming.LastTimingPart = partNumber
	carDataTrackTiming.TimingPartTimes[partNumber] = TIME_TOUCHED
	
	----------- IMPORTANT FOR ORDERING
	carDataTrackTiming.TotalParts = (carDataTiming.LapCount * TrackTiming.TotalParts) + partNumber
	
	if carDataTrackTiming.BestTimes[1] and carDataTrackTiming.BestTimes[partNumber] then
		local bestDiff = carDataTrackTiming.BestTimes[partNumber] - carDataTrackTiming.BestTimes[1]
		local currentDiff = carDataTrackTiming.TimingPartTimes[partNumber] - carDataTrackTiming.TimingPartTimes[1]
		
		carDataTrackTiming.Delta = currentDiff - bestDiff
	end
	
	carDataTrackTiming = nil
	carDataTiming = nil
	carData = nil
end

function RegisterPart(part, partNumber)
	part.Touched:Connect(function(otherObject: Part)
		local carModel = otherObject:FindFirstAncestor("A-Chassis")
		if not carModel then return end

		local occupant = carModel:FindFirstChild("DriveSeat", true).Occupant
		if not occupant then return end
		
		local player = game.Players:GetPlayerFromCharacter(occupant.Parent)
		local car = Data:GetPlayerCar(player and player.UserId or 0)
		if not car then return end
		local carumber =  car.CarNumber
		TrackTimingLogic(carumber, partNumber)
	end)
end

function TrackTiming:Load()
	debug.setmemorycategory("SystemsTRACKTIMING")
	
	for _, part in script.Parts:GetChildren() do
		local partNumber = tonumber(part.Name)
		RegisterPart(part, partNumber)
	end
	
	TrackTiming.TotalParts = #script.Parts:GetChildren()
end

return TrackTiming

Here is a picture of the graph:

Using .Touched here is probably not the best idea. If the car is made of multiple parts with CanTouch enabled, then this event might be firing multiple times for a single vehicle.

If it is then a simple approach is to use a cooldown table to track when the part Parent touched and if the delta since last touched is les than the expected time between touch points.

An alternate might be use a module such as SimpleZone .

I’m not saying that is the root of the problem but it is what immediately stands out to me.

Oh my goodness, I never though about the CanTouch property. I knew the fact that the car was firing .Touched events multiple times was an issue but I could not think of a way around it. Thank you for reminding me.

As far as not using .Touched - I will definitely look into SimpleZone. Thank you so much for your feedback and suggestions, it’s of great help.

I see a few things here, but I’m just going to point out the two most likely causes..

#1..
Data:TriggerDataSend() Called Every 0.25 Seconds
The function script.CarsUpdate:FireAllClients(Data.Cars) sends ALL car data to every client 4 times per second. With 29 cars, each with potentially large nested tables, this is likely the source of your 9GB/s network traffic.

#2..
150 timing parts (in TrackTiming)
300 corner cut parts (in Timing)
450+ events that could be firing multiple times per second per car, with no deboucing.

1 Like

You can compress the data you send to clients, should exponentially decrease the network bandwidth (as compared to raw uncompressed data). Here’s how you can do so:

First I wanna just focus on what :CreateCar() modifies from your template.

CarTemplate = {
	TeamName = nil:: string,
	CarClass = nil:: string,
	CarNumber = nil:: number,
	CarName = nil:: string,
	AvailableMembers = {}:: {number},
	LiverID = nil:: number
},

Two things to take in mind:

  • Strings cost #n + ~1 bytes, so for example, “hello world” would cost 11 + ~1 = ~12 bytes
  • Numbers are 64-bit floating points on default and cost 8 bytes, so for example, 1, 1.0001, and 10000 all equal to 8 bytes

A lot of these strings, such as TeamName and CarClass, can be just referenced as numbers. The benefit of this is we can compress numbers into a format such as 8-bit integers through buffers. This is not a comprehensive tutorial, just trying to show you what you can do with buffers, so you’re going to have to do research to use buffers.

local TeamNameList = {
	["VOLT Racing"] = 1,
	["Redwing Racing"] = 2,
	...
}

-- "VOLT Racing" costs ~12 bytes
script.CarsUpdate:FireAllClients("VOLT Racing") -- costs ~12 bytes!

-- 1 (as an 8-bit integer) costs only 1 byte. yes, from ~12 to 1 byte.
local dataBuffer = buffer.create(...)
buffer.writeu8(dataBuffer, 0, TeamNameList["VOLT Racing"])
script.CarsUpdate:FireAllClients(dataBuffer) -- costs only 1 byte!

You can also compress your numbers, such as CarNumber, into 32-bit, 16-bit, and 8-bit integers through buffers. There is also buffer.writebits() which lets you write in specific bits ranging from 0 to 32.

BIG DISCLAIMER. Keep in mind that the amount of bits equals to how big your numbers can get, here’s a table of bits and their respective ranges.

1 Like

Keep in mind that dictionary indexes also cost bandwidth, this is why you are recommended to use arrays over dictionaries when sending data. Here’s an example:

-- dictionary
CarTemplate = {
	TeamName = "Test" -- ~9 bytes ("TeamName") + ~5 bytes ("Test") = ~14 bytes
}

-- array
CarTemplate = {
	"Test" -- ~5 bytes
}

It will be less readable, but most of the time, the bandwidth you save just by opting out of dictionaries is worth the trouble.

1 Like