Something just doesn’t add up right here. Isn’t it a bit overboard to call on datastore more than 600 times when just once is enough? I’m using ProfileService but when I look at DeveloperConsole I’m seeing DataStore methods like UpdateAsync get called more than 600 times and then increasing.
Is there something wrong with my ModuleScript?
--- The Stats the player will start with ---
local DefaultTable = {
Orientation = "Portrait",
cash = 0;
cps = 100;
click = 50;
PlotOwned = {},
House = {
SuburbanHouse = 1
},
BuildingOutput = {0.1,0.3,1,5,35,};
totalBuildings = {0,0,0,0,0,0};
BuildingCost = {1,100,1100,12000};
FinishedTutorial = false,
}
---- Variables ----
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local ProfileService = require(script.ProfileService)
local ProfileStore = ProfileService.GetProfileStore(
"GlobalPlayerData",
DefaultTable
)
---- Player Data Server ----
local Profiles = {}
local DataManager = {}
function DataManager:Get(player)
local profile = Profiles[player]
if profile then
return profile.Data
end
end
local function clock(profile) -- The actual clock that handles the money
RunService.Heartbeat:Connect(function(DeltaTime)
profile.cash += profile.cps * DeltaTime
end)
end
local function Clock(profile)
local CashFlow = ReplicatedStorage.CashFlow
while true do
wait(10)
end
end
local function SetScreenOrientation(player,Orientation)
if player or Orientation then
local ToEnumOrient = Enum.ScreenOrientation[Orientation]
ReplicatedStorage.SetScreen:FireClient(player,ToEnumOrient)
end
end
----- The important stuff related to ProfileService -----
local function PlayerJoining(player)
local profile = ProfileStore:LoadProfileAsync(
"User_" .. player.UserId,
"ForceLoad"
)
if profile then
profile:Reconcile()
profile:ListenToRelease(function()
Profiles[player] = nil
player:Kick("Player: " .. tostring(player) .. "left the game")
end)
if player:IsDescendantOf(Players) then
Profiles[player] = profile
SetScreenOrientation(player, profile.Data.Orientation)
coroutine.wrap(clock)(profile.Data)
else
profile:Release()
end
else
-- There was an error when retrieving player DataStore, Release will not work
player:Kick("Error when retrieving" .. tostring(player) .. "GameData")
end
end
local function PlayerLeft(player)
local profile = Profiles[player]
if profile then
profile:Release()
end
end
function DataManager:PlayerJoin()
Players.PlayerAdded:Connect(PlayerJoining)
end
function DataManager:PlayerLeaving()
Players.PlayerRemoving:Connect(PlayerLeft)
end
function DataManager:Proof() -- Apparently players joining faster than scripts run is a thing.
for _,player in ipairs(Players:GetChildren()) do
coroutine.wrap(PlayerJoining)(player)
end
end
return DataManager