Fixing a cache system for my modded Okeanskiy Terrain Generation

What do you want to achieve?

From my last topic, debugger57 had suggested that I develop a cache system to prevent trash from generating differently for diffferent clients. I had developed a rudimentary cache system, but…

What is the issue?

The code I wrote got dangerous. It lagged my game very badly and the errors concern C stack overflows. At first, the game ran smoothly. Then, the game lagged as if some bad exploiter went in and wreaked havoc.

What solutions have you tried so far?

So far, the dangerous code I developed is as follows:

CacheLocal

ReplicatedStorage = game:GetService("ReplicatedStorage")
CacheAdd = ReplicatedStorage.Events:WaitForChild"CacheAdd"
Cache = require(ReplicatedStorage:WaitForChild("Cache"))
TrashModels = game.Workspace:WaitForChild("TrashModels")

TrashModels.ChildAdded:Connect(function(child)	--	Whenever a new trash model is added to the Workspace...
	if child:IsA("Model") then					--	If it really is a trash model...
		local PrimaryPart = child.PrimaryPart	--	its data, which includes
		local ModelName = child.Name			--	its name and
		local pos = PrimaryPart.Position		--	position, more specifically
		local X = pos.X							--	its X
		local Z = pos.Z							--	and Z coordinates,
		local data = table.pack(ModelName,X,Z)	--	will be packed into a table.
		CacheAdd:FireServer(data)				--	Said data is sent to the server.
	end
end)

CacheServer

ReplicatedStorage = game:GetService("ReplicatedStorage")
Cache = require(game:GetService'ReplicatedStorage':WaitForChild'Cache')
CacheAdd = ReplicatedStorage:WaitForChild("CacheAdd")

m = {__newindex = function(t,k,v)
	t[k] = v
end}
setmetatable(Cache,m)							--	We'll need this to add to the module.

function cacheAdd(player,data)					--	Instructions:
	local ModelName,X,Z = table.unpack(data)	--1) Unwrap the packaged data.
	local key = tostring(X..","..Z)				--2) Write down the key. (Say, "1,1")
	Cache[key] = tostring(ModelName)			--3) Designate the key as the trash's name.
end

CacheAdd.OnServerEvent:Connect(cacheAdd)		--	Do these instructions when the data arrives.

Something is VERY wrong with my code. What am I missing? What do I remove? Do I have to mod the metamethod? Do you have better solutions in mind?

Dont use table functions (pack, unpack). Use a hash table with key-value pairs.

1 Like

UPDATE

I finally figured out what was wrong with my code, and it has something to do with the added metatable. All I just have to do is the following:

Firstly, debugger57 suggested that I use neither table.pack() nor table.unpack(). As such, I did so in game.StarterPlayer.StarterPlayerScripts.CacheLocal:

ReplicatedStorage = game:GetService("ReplicatedStorage")
CacheAdd = ReplicatedStorage.Events:WaitForChild"CacheAdd"
Cache = require(ReplicatedStorage:WaitForChild("Cache"))
TrashModels = game.Workspace:WaitForChild("TrashModels")

TrashModels.ChildAdded:Connect(function(child)
	if child:IsA("Model") then
		local PrimaryPart = child.PrimaryPart
	--	local ModelName = child.Name			--	<- I realized I did not need this.
		local pos = PrimaryPart.Position
		local X = pos.X
		local Z = pos.Z
	--	local data = table.pack(ModelName,X,Z)	--	<- This is commented out.
		CacheAdd:FireServer(child,X,Z)			--	<- I realized that I can send
	end											--	Instances through RemoteEvents.
end)

Afterwards, I got rid of the unnecessary metatable in game.ServerScriptService.CacheServer, and did a few more mods:

ReplicatedStorage = game:GetService("ReplicatedStorage")
Cache = require(game:GetService'ReplicatedStorage':WaitForChild'Cache')
CacheAdd = ReplicatedStorage:WaitForChild("CacheAdd")
--[[	No need for this.
m = {__newindex = function(t,k,v)
	t[k] = v
end}
setmetatable(Cache,m)
]]
function cacheAdd(player,data)				
--	local ModelName,X,Z = table.unpack(data)	<-	As the function in CacheLocal
	local key = tostring(X..","..Z)				--	was changed, this line was
	Cache[key] = tostring(ModelName)			--	removed.
end

CacheAdd.OnServerEvent:Connect(cacheAdd)