Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Accurate Data Store Player Count?

Asked by 7 years ago

I have a place with a bunch of separate universes and I can't get player count right. This script is in every place and when I call it from the lobby it never returns accurate information. Is there something wrong with my code? By accurate I mean it goes negative sometimes and other times it just doesn't count down and the number just keeps increasing.

local Data = game:GetService('DataStoreService'):GetDataStore('PlayerCount')

local Datas = Data:GetAsync(game.PlaceId)
if Datas == nil or 0 >= Datas then
    Data:SetAsync(game.PlaceId,0)
end

game.Players.PlayerRemoving:connect(function()
    Data:SetAsync(game.PlaceId,Data:GetAsync(game.PlaceId)-1)
end)

game.Players.PlayerAdded:connect(function()
        Data:SetAsync(game.PlaceId,Data:GetAsync(game.PlaceId)+1)
end)

game.OnClose = function()
    for i,v in pairs(game.Players:GetChildren()) do
        Datas:SetAsync(game.PlaceId,Data:GetAsync(game.PlaceId)-1)
    end
end

1 answer

Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

A data store is not meant to be used this way as there are limitations such as:-

  • Key access limits, when you try to access / set the same key multiple times (Currently 10 requests per key / min)

  • GetAsync requests are cached for 10 seconds unless you set the key then it is cleared

  • Request limitations, for both set and get requests the request limits change for the number of players, '60 + numPlayers * 10' (These limits may change over time)

Other notes:-

game.OnClose should not be used as there is a better method now available BindToClose which does not overwrite the function allowing you to stack multiple functions to the closed event.

This issue can be caused by one or more of the limitations that a data store has ( there are also other that are not mentioned in the list above). To resolve this you could:-

  • Use the HttpService as this has a much higher request limit of 500 per min but this data would then need to be stored on a third party service.

  • Limit the player update times to something like 5 minutes then store both the time and player count e.g. game.Players.NumPlayers, this data can then be used in the lobby.

Ad

Answer this question