I have a player counter for other places within my game. At first, it wasn't that big of an issue that we were dealing with after a few edits I had done. We were only off +1-2 on the DataStore. But then, it started to get worse. Like yesterday, we had 103 people visit, which made it say, today, that there were 8 people on when only 2 were on. Changing the key over and over is starting to not work anymore because it just goes back to the same problem a few hours later. Is there any way we could make it so this cannot happen as much? It's starting to get really annoying. I have no clue how to use HttpService for doing this, so that's out of the image. The only language I know how to code in is lua.

Here's my current code for when the player joins and leaves:

local DataStore = game:GetService("DataStoreService"):GetDataStore("Cities1.111")
local Albany = DataStore:GetAsync("Albany")

if Albany == nil or type(Albany) == "string" then
	print"updated datastore"
	DataStore:UpdateAsync("Albany", function(oldValue)
	local newValue = 0
	return newValue
	end)
end


game.Players.PlayerAdded:connect(function(player)
	
	
	local success, message = pcall(function()
		DataStore:IncrementAsync("Albany", 1)
	end)
	if not success then
		 print("An error occurred: " .. message)
	end
end)


game.Players.PlayerRemoving:connect(function(oldValue)
	local success, message = pcall(function()
		DataStore:IncrementAsync("Albany", -1)
	end)
	if not success then
		 print("An error occurred: " .. message)
	end
end)


game.OnClose = function()
	wait(5)
end

2 comments

0

There are limitations to how many times you can set the same key, 10 per min. The data store is not meant to be used this way especially if you are using the same key on multiple game instances.

1

I feel like a datastore like that is guaranteed to fail sometimes. You should just keep track of players in each server, and update the datastore at once in a pretty big random interval like random(60,120)