Im working on a module that passes information from the server to client, along with some other stuff to make it work better.
I ran into an issue today where a value called CurrentData Doesnt update. This value does update, but once it has items inside of it that also have items it seems to just not update, that or it doesnt update after the second time.
I’ve done lots of debugging and have found that when it sends the information to the client the right information is passed, I even put a while loop to print the CurrentData, it prints the right information, but when I try to get this information from the module it returns the data that was there before it was set to what it is now.
How do I return the data to whats accessing the module? Metatables:
-- || This code fetches the information ||
local MT = {
__index = function(_, Key)
warn(CurrentData,RunService:IsClient())
if Container[Key] ~= nil then
return Container[Key]
elseif CurrentData[Key] ~= nil then
return CurrentData[Key]
elseif script:GetAttribute(Key) ~= nil then
return script:GetAttribute(Key)
else
return nil
end
end,
So if anyone has any idea as to why it isnt updating it would be very nice, as this is slowing me down tremendously
Thanks!
Full Code
-- // Services //
local Players = game:GetService('Players')
local RunService = game:GetService('RunService')
local RStorage = script:FindFirstAncestorOfClass('ReplicatedStorage')
-- // Events //
local MessageEvent = script:FindFirstChild('GlobalMessage') or script.Parent:WaitForChild('GlobalMessage')
-- // Modules //
local AttributeTypes; if RunService:IsServer() then AttributeTypes = require(126586171540014) end
local Signals = require(script.Parent.Utilities.Signal)
-- // Types //
export type Values = {
GlobalChanged:Signals.Signal,
Update: (self) -> nil,
}
local Test:Values = {}
-- // Globals //
local LastSentData = {}
local CurrentData = {}
local Connections = {}
local Debounce = false
local TriggerKey = 0
local MainScript = 0
local Container:Values = {
GlobalChanged = Signals.new()
}
local Display:Values = {}
-- // Functions //
local function DeepCheck(Tab1, Tab2) : boolean
local Equal = true
for Key, Value in Tab1 do
if not Tab2[Key] then Equal = false; continue end
if typeof(Value) == 'table' then
if not DeepCheck(Value,Tab2[Key]) then
Equal = false
end
else
if Tab2[Key] ~= Value then
Equal = false
end
end
end
return Equal
end
local function MessagePlayer(Player:Player)
if RunService:IsServer() then
MessageEvent:FireClient(Player,CurrentData)
end
end
local function UpdatePlayers()
TriggerKey += 1
local Key = TriggerKey
-- || Debounce ||
if Debounce then
repeat task.wait(1) until not Debounce or TriggerKey ~= Key
if TriggerKey ~= Key then return end
end
-- || Actual Stuff ||
Debounce = true
local DataUpdated:boolean = DeepCheck(CurrentData,LastSentData)
if DataUpdated then -- || Data isnt the same ||
print(CurrentData)
LastSentData = CurrentData
MessageEvent:FireAllClients(CurrentData)
end
task.wait(1)
Debounce = false
end
-- // Finish Up //
MainScript += 1
if MainScript == 1 and RunService:IsServer() then -- || This is the runs after being Required for the First Time ||
MessageEvent.Parent = script.Parent
LastSentData = {}
-- || Add Current Players ||
for _, player in Players:GetPlayers() do
MessagePlayer(player)
end
-- || Send to New Players. ||
Players.PlayerAdded:Connect(MessagePlayer)
elseif RunService:IsClient() then
-- || Detect New Data ||
MessageEvent.OnClientEvent:Connect(function(Data)
print('UPDATED',Data)
for Key, Value in CurrentData do
if Value ~= Data[Key] then
Container.GlobalChanged:Fire(Key,Value,Data[Key])
end
end
CurrentData = Data
end)
end
-- || Connections ||
script.AttributeChanged:Connect(function(Key:string)
Container.GlobalChanged:Fire(Key,nil,script:GetAttribute(Key))
end)
-- || Metatable ||
local MT = {
__index = function(_, Key)
warn(CurrentData,RunService:IsClient())
if Container[Key] ~= nil then
return Container[Key]
elseif CurrentData[Key] ~= nil then
return CurrentData[Key]
elseif script:GetAttribute(Key) ~= nil then
return script:GetAttribute(Key)
else
return nil
end
end,
__newindex = function(_, Key, Value)
if RunService:IsServer() and MainScript == 1 then
local Original
if AttributeTypes:IsSupported(Value) then
Original = script:GetAttribute(Key)
CurrentData[Key] = nil
script:SetAttribute(Key,Value)
else
Original = CurrentData[Key]
CurrentData[Key] = Value
if script:GetAttribute(Key) then
script:SetAttribute(Key,nil)
end
end
Container.GlobalChanged:Fire(Key,Original,Value)
task.spawn(UpdatePlayers)
end
end,
}
-- || Functions ||
function Container:Update()
if RunService:IsServer() then
task.spawn(UpdatePlayers)
end
end
local function cheese()
if RunService:IsClient() then
while task.wait(2) do
print(CurrentData)
end
end
end
task.spawn(cheese)
-- || Return ||
setmetatable(Display,MT)
return Display