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
