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?