Variable "CurrentData" Isnt updating?

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

Don’t you want to make DataUpdated equal to not DeepCheck... since DeepCheck returns whether or not the two tables are equal? If they are equal, DataUpdated will be true, which is what you don’t want

3 Likes

Huh I didnt realize I did that! Thanks!

3 Likes

For whatever reason now whenever I check, LastSentData is equal to CurrentData even though LastSentData hasnt even been set yet?

Bro my code is so weird

2 Likes

Where in the script are you seeing this?

1 Like

right after, and before I call DeepCheck

Wait I figured it out, its cause I set LastSentData to CurrentData, meaning anytime CurrentData changes, well LastDataSent is equal to it, so it updates too. Lemme fix that.

The original problem does still stand though

1 Like

2 things:

1- are you sure the client connected to the remote event at the time it fires?
2- shouldn’t this:
for Key, Value in CurrentData do
be
for Key, Value in Data do?
When you iterate over the initial CurrentData, it won’t have any entries so no iterations will happen

1 Like

Oh good point, thanks for catching that.

Im pretty sure that there is only one local script connected to the RemoteEvent, of which, the print that gets triggered when it updates is also there, and it does print.

Ok, so just to clarify the issue now is that when you try doing Display.someIndex, it isn’t returning the entry in CurrentData?

Can you show how you’re indexing it, and does the warn happen at all?

Ok heres me referencing in, and I am sure for a fact (Ive printed it and what not) it is trying to get the right value, even so the time I warn CurrentData in the index function still doesnt print the right thing):

local function GetValueFromText(Text:string) : any
	if not Text then return '' end
	local Value = ''
	
	local List = string.split(Text,'.')
	local Parent = List[1]
	
	if Parent == 'Global' then
		Value =  Globals[List[2]]
		
		if typeof(Value) == 'table' then
			Value = #Value
		end
	end
	
	return Value or 'N/A'
end

Ok probably should’ve specified this part. So, the data is getting updated, of which the Data, or CurrentData is set to {Ready = {[1] = UserIdHere}, Players = {}}, except any time I attempt to access it CurrentData.Ready is well just blank, or {}.

Warning the CurrentData inside of the index function prints the CurrentData except Ready is empty.

Okay, so when you print UPDATED, Data on the client, are the user ids present there or no? If not, can you show what the CurrentData is on the client and on the server at the time the remote event is fired?

Yes when I print that the Id’s are there, so it appears the client is receiving the right information, its just not updating?

Yeah I’m not too sure then, it looks like it should be updating, I can’t see any reason it wouldn’t be.

1 Like

I figured it out, I was firing the GlobalChanged event too soon (its an event to tell when a value changes) I fired it before I changed the value, causing the error

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.