DataStore always returning same key, even after it changes

i’m working on a secret project right now, and its relating to cross-server communication

one server saves a key, then says to the other server “grab the key”. however, for some reason, after the first fetch [the first fetch caches it or smth, idk?? i disabled the caching], the key is always the same. why is this, how do i make it change?

local connect = {}

local storage = require(script.Parent.Storage)
local ds = require(script.Parent.DSMgr)
local config = require(script.Parent.Parent.Config)
local minstance = require(script.Parent.Parent.Modules.Minstance)

function connect.GetReplication(params, player)
	local i = 0
	local targetserverid = params.TargetServer
	local sourceserverid = storage.JobId
	local event = require(script.Parent.DSMgr).HookMessage("ServerConnectionTransmission")
	task.wait(3)
	ds.SendAndYieldAsync({
		Type = "ConnectingToServer_5", --signify to start stream
		Content = {
			UserID = player.UserId,
			RefreshRate = config.RefreshRate
		}
	}, "", 0, targetserverid)
	local prevmapcontent
	event.Event:Connect(function(content)
		if content.TargetJobId == targetserverid then
			ds.SendAndYieldAsync({
				Type = "ServerConnectionTransmission",
				Content = {
				}
			}, "", 0, targetserverid)
			i += 1
			if i == 3 then
				task.wait(20)
			end			
			local mapcontent = 	ds.GetAsync({
				DataStore = config.DataStore,
				Key = "Connection_" .. sourceserverid
			})
			print(prevmapcontent == mapcontent)
			if mapcontent ~= nil then
				prevmapcontent = mapcontent
				mapcontent = minstance.DeserializeInstance(mapcontent)
				if game.Workspace:FindFirstChild("SECRET_MapContent") then
					game.Workspace:FindFirstChild("SECRET_MapContent"):Destroy()
				end
				mapcontent.Name = "SECRET_MapContent"
				mapcontent.Parent = game.Workspace
			end
		end
	end)
end

return connect

datastore code:

function DS.GetAsync(params)
	local cacheoptions = Instance.new("DataStoreGetOptions")
	cacheoptions.UseCache = false
	local success, content = pcall(function()
		if not params.Key
			or not params.DataStore
		then
			return nil
		end
		local ds = game:GetService("DataStoreService"):GetDataStore(params.DataStore)
		local function fetch(amount)
			local success, content = pcall(function()
				if require(script.Parent.Parent.Config).LogPrints == true then
					print("🏔️ | 🗃️ | Fetching key " .. params.Key .. " with attempt count " .. amount)
				end
				return ds:GetAsync(params.Key, cacheoptions)
			end)
			if success then
				if content == nil then
					content = ""
				end
				return content
			else
				amount += 1
				if amount >= 10 then
					return nil
				end
				fetch(amount)
			end
		end
		return fetch(0)
	end)
	if not success then
		return false
	else
		return content
	end
end

if anyone needs clarification, just ask because i am ill and my wording is super funny

i have no clue why it’s not updating

do you mind sharing the code for ds.SendAndYieldAsync, please?

alright, the name of my project is redacted with “SECRET”, but besides hat, the code is identical

function DS.SendAndYieldAsync(params1, tag, limit, targetid)
	local connection
	local result = false
	params1.SenderJobId = storage.JobId
	params1.TargetJobId = targetid
	connection = game:GetService("MessagingService"):SubscribeAsync("SECRET", function(raw)
		local content = game:GetService("HttpService"):JSONDecode(raw.Data)
		if content.TargetJobId == nil then return end
		if content.Type == tag
			and content.TargetJobId == storage.JobId
			and content.SenderJobId ~= storage.JobId
		then
			if require(script.Parent.Parent.Config).LogPrints == true then
				print("🏔️ | 📡 | SECRET PROJECT CANNOT SHARE AME YETcommunicated with server " .. content.SenderJobId .. "successfully. Here's the logs:" 
					.. game:GetService("HttpService"):JSONEncode(content))
			end
			result = content
			connection:Disconnect()
		else
		end
	end)
	game:GetService("MessagingService"):PublishAsync("SECRET", game:GetService("HttpService"):JSONEncode(params1))

	task.spawn(function()
		task.wait(limit)
		result = nil
	end)

	repeat task.wait() until result ~= false

	return result
end

local connect = {}

local storage = require(script.Parent.Storage)
local ds = require(script.Parent.DSMgr)
local config = require(script.Parent.Parent.Config)
local minstance = require(script.Parent.Parent.Modules.Minstance)

function connect.GetReplication(params, player)
	local i = 0
	local targetserverid = params.TargetServer
	local sourceserverid = storage.JobId
	local event = require(script.Parent.DSMgr).HookMessage("ServerConnectionTransmission")
	task.wait(3)
	ds.SendAndYieldAsync({
		Type = "ConnectingToServer_5", --signify to start stream
		Content = {
			UserID = player.UserId,
			RefreshRate = config.RefreshRate
		}
	}, "", 0, targetserverid)
	local prevmapcontent
	event.Event:Connect(function(content)
		if content.TargetJobId == targetserverid then
			ds.SendAndYieldAsync({
				Type = "ServerConnectionTransmission",
				Content = {
				}
			}, "", 0, targetserverid)
			i += 1
			if i == 3 then
				task.wait(20)
			end			
			local mapcontent = 	ds.GetAsync({
				DataStore = config.DataStore,
				Key = "Connection_" .. sourceserverid
			})
			print(prevmapcontent == mapcontent)
			if mapcontent ~= nil then
				prevmapcontent = mapcontent
				mapcontent = minstance.DeserializeInstance(mapcontent)
				if game.Workspace:FindFirstChild("SECRET_MapContent") then
					game.Workspace:FindFirstChild("SECRET_MapContent"):Destroy()
				end
				mapcontent.Name = "SECRET_MapContent"
				mapcontent.Parent = game.Workspace
			end
		end
	end)
end

return connect

what piece of code do you believe should set the datastore key to the value that you want? also is your DS module made by you or a publicly available module?

my DS module is made by me, that piece of code is from the receiving end. in the other server, it does this:

function connect.StartReplication(params)
	--TODO: modify minstance to hide characters
	local targetserverid = params.TargetServer
	local sourceserverid = storage.JobId
	local refreshrate = params.RefreshRate
	local connected = storage.Connected --TODO: connection severing
	local event = require(script.Parent.DSMgr).HookMessage("ServerConnectionTransmission")
	if require(script.Parent.Parent.Config).LogPrints == true then
		print("🏔️ | 📜 | Connected to host " .. targetserverid)
	end
	local function waitforevent()
		local done = false
		local tempconnect
		tempconnect = event.Event:Connect(function(content)
			if content.TargetJobId == targetserverid then
				done = true
			end		
		end)
		local num = 0
		repeat
			task.wait(1)
			num += 1
		until done == true or num == refreshrate + 1
		tempconnect:Disconnect()
		if done == false then
			storage.Connected = nil
			connected = false
		end
	end
	repeat
		if require(script.Parent.Parent.Config).LogPrints == true then
			print("🏔️ | 📜 | Refreshing data for host " .. targetserverid)
		end
		local compile = minstance.SerializeInstance(game.Workspace)
		ds.SetAsync({
			DataStore = config.DataStore,
			Key = "Connection_" .. sourceserverid,
			Value = compile
		})
		task.wait(refreshrate)
		task.spawn(function()
			waitforevent()
		end)
		ds.SendAndYieldAsync({
			Type = "ServerConnectionTransmission",
			Content = {
			}
		}, "", 0, targetserverid)
		local hostclone = minstance.DeserializeInstance(compile)
		hostclone.Parent = game.ReplicatedStorage
		local otherthing = ds.GetAsync({
			DataStore = config.DataStore,
			Key = "Connection_" .. sourceserverid,
		})
		print(compile == otherthing)
		task.wait(refreshrate)
	until connected == false
	if require(script.Parent.Parent.Config).LogPrints == true then
		print("🏔️ | 📜 | Disconnected from SECRET communications")
	end
	storage.Connected = nil
end

sorry, forgot to specify it earlier

we’re getting there, what’s the code for ds.SetAsync?

function DS.SetAsync(params)
	local success, content = pcall(function()
		if not params.Key
			or not params.DataStore
			or not params.Value
		then
			return nil
		end
		local ds = game:GetService("DataStoreService"):GetDataStore(params.DataStore)
		local function fetch(amount)
			local success, content = pcall(function()
				if require(script.Parent.Parent.Config).LogPrints == true then
					print("🏔️ | 🗃️ | Setting key " .. params.Key .. " with attempt count " .. amount)
				end
				return ds:SetAsync(params.Key, params.Value)
			end)
			if success then
				return content
			else
				amount += 1
				if amount >= 10 then
					return nil
				end
				fetch(amount)
			end
		end
		return fetch(0)
	end)
	if not success then
		return false
	else
		return content
	end
end

i assume that you are trying to save the character’s instance when they leave, and insert it back when they join back, correct?

ds.SetAsync({
			DataStore = config.DataStore,
			Key = "Connection_" .. sourceserverid,
			Value = compile
		})

based on the key part, i think the game is trying to save the value to the server’s id and not the player’s id

also what is “other server”? a different server script or an actual server, the type that you can join

a entirely different server that you can join

what i’m trying to do is, save the first server’s state and display what the state looks like in the OTHER server. however it never updates for some reason on the 2nd server

are you trying to make a server browser? in that case i’d suggest using MessagingService, and broadcasting a message on the start of a server. then the rest of the servers can have their own table with all the server data, and not a datastore.

when a new server is created it:
broadcasts its existence and static data (like location) to others
broadcasts an event to ask the rest of the servers for their existence

about every minute the server should:
broadcast nonstatic data (like the amount of players)

when the server is about to close:
broadcast its death

note i did not test this implementation, but i believe it will work

again, this
i did not specify smth though

i’m using minstance to serialize a map, then in the other server load that map. it loads normally the first time, but every time AFTER the first time, it just loads the first copy

why are you trying to serialize the map? is the map (probably a mountain based on the prints) very user destructible? if it’s procedurally generated i think sending just the seed would be better.

also

local refreshrate = params.RefreshRate
local connected = storage.Connected --TODO: connection severing
local event = require(script.Parent.DSMgr).HookMessage("ServerConnectionTransmission")

i think the reason it stops updating after 1 request is because connected immediately becomes false, you can try setting it to true in the script manually.]
the behaviour of repeat until is that even if the condition goes to true immediately, it will still run it atleast once. unlike while do which if the condition is true immediately, it doesn’t run the code at all.

it’s not seed-related at all, its for a secret purpose which i can’t say
p.s. nice catch on the “Mountain” part, i’ll probably send a message in this thread when the preview releases

to get back on topic though…

	until connected == false

the packets will always send to the second server [it isn’t a problem with that, i’ve double checked]. i think it’s an issue with the datastore, because when the second server picks the key up, it is exactly identical to the last, yet on the first server it isnt

odd. can you do the following test with your ds module?

  1. print contents of key “A”
  2. set key “A” to 1
  3. set key “A” to 3 (with a small delay after step 1)

if step 1 prints:
nil on the first try (or whatever your module uses as an equivalent)
3 on the second try

that means its probably not a problem with your module

alright, i’ll do it tmrw, because i got to get some sleep
thank you

so, it turns out, umm

i was extremely sleep deprived last night, so i didnt notice the MULTIPLE ERRORS IN MY CODE where i set it to the wrong key [the first server’s id, instead of the second server’s id]

it also wasn’t communicating properly

yeah it was a problem with me and my datastores, sorry for wasting your time :sweat_smile:

i’ll leave this post open though for when i do launch the secret project, so i can notify people

game better be good :folded_hands::folded_hands::folded_hands::folded_hands::folded_hands::folded_hands::folded_hands::folded_hands::folded_hands::folded_hands::folded_hands::folded_hands:

next time we should use assistant to search the code in addition too, grav, lol

It’s always better to have another set of eyes!!