DataStore not Recognizing Dictionary as nil

I’m currently scripting a DataStore system that allows players to join different servers that are in a different place under the same game.

To give an idea on what I am wanting to achieve, here it is.

Place A is the main game, where the datastore displays the list of servers of the other place (Every 30 seconds). Place B uses the same DataStore. In each server, when the server starts the DataStore adds a dictionary containing the server data, and the dictionary is removed when the server owner leaves.

The issue I’m having while the script in Place A does recognize when a player joins a created server in Place B, Place A will think that the player is still in there when leaving Place B. Here is a gif.

Just to debug, I had all of the contents of the DataStore printed every second (I know this is a bad practice due to budget reasons, but it’s only to debug and is meant to be 30 seconds). I am not in Place B yet the script still thinks I am.

https://gyazo.com/92317528a79aad815d25d73338beb583.mp4

Here is the script on Place-A that loads the server data.

local function UpdateList()
	print("Housing Server: Updating list!")
	
	local ServerList;
	
	local Good, Bad = pcall(function()
		ServerList = ServersDataStore:GetAsync(Key)
	end)
	
	if not Bad then
		for _, ServerInfo in pairs(ServerList) do
			print(ServerInfo.OwnerName)
		end
	end
end

while wait(Server_List_Refresh_Time) do
	UpdateList()
end

And here is the code that changes the data in Place B.

local function RequestServerListUpdate()
	if PlaceOwner ~= nil then
		local SuccessfullyLoaded = false

		for i = 1, Server_List_Retries do
			
			local Good, Bad = pcall(function()
				ServerListData = ServersDataStore:GetAsync(Key)
			end)
			
			if not Bad then
				SuccessfullyLoaded = true
				print "Loaded Server DataStore!"
				break
			else
				warn(Bad)
			end
		end	
		
		ServerListData[PlaceOwner.Name] = {OwnerName = PlaceOwner.Name, ServerId = game.PrivateServerId, PlayerCount = #Players:GetPlayers()}

		for i = 1, Server_List_Retries do
			
			local Good, Bad = pcall(function()
				ServersDataStore:SetAsync(Key, ServerListData)
			end)
			
			if not Bad then
				print "Updated Server DataStore!"
				break
			else
				warn(Bad)
			end
		end
		
	end
end

local function OnPlayerAdded(Player)
	if PlaceOwner == nil then
		print("Owner set")
		PlaceOwner = Player		
		RequestServerListUpdate()
	end	
end

local function OnPlayerRemoved(Player)
	if Player == PlaceOwner then
		
		ServerListData[PlaceOwner.Name] = nil
		print(ServerListData[PlaceOwner.Name])
		
		for i = 1, 10 do
			
			local Good, Bad = pcall(function()
				ServersDataStore:SetAsync(Key, ServerListData)
			end)
			
			if not Bad then
				break
			end
		end
	end
end








Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoved)

So this is an issue with saving? Have you given the datastores enough time to save when the server owner leaves, by using BindToClose?

The data may not be saving before the server closes, which could be causing the problem rather than the script itself. I would recommend checking if the game is running on studio before setting a BindToClose function. You could probably get away with the BindToClose function simply yielding/waiting for 10-30 seconds before letting the game close.

If testing in studio also make sure studio APIs are enabled but I doubt this is affecting anything as it sounds like you’re using actual servers as opposed to local servers.

1 Like

I have BindToClose implemented on my script already. Also, I am in fact using Studio at the moment for both places. Studio API is enabled.