-- Setup table that we will return to scripts that require the ModuleScript.
local PlayerStatManager = {}
-- Create variable for the DataStore.
local DataStoreService = game:GetService('DataStoreService')
local playerData = DataStoreService:GetDataStore('PlayerData34324132933')
-- Create variable to configure how often the game autosaves the player data.
local AUTOSAVE_INTERVAL = 60
-- Number of times we can retry accessing a DataStore before we give up and create
-- an error.
local DATASTORE_RETRIES = 3
-- Table to hold all of the player information for the current session.
local sessionData = {}
local modulescripts = workspace:WaitForChild("ModuleScripts")
local requestDataRF = modulescripts:WaitForChild("RequestData")
local sendDataRF = modulescripts:WaitForChild("SendData")
local dataUpdatedRF = modulescripts:WaitForChild("DataUpdated")
local dataUpdatedRE = modulescripts:WaitForChild("DataUpdatedEvent")
local uploadDataRE = modulescripts:WaitForChild("UploadData")
local requestSessionDataRF = modulescripts:WaitForChild("RequestSessionData")
function requestDataRF.OnServerInvoke(player, statName)
if sessionData[player] == nil then
repeat
wait(0.5)
until sessionData[player] ~= nil
end
return sessionData[player][statName]
end
function requestSessionDataRF.OnServerInvoke(player)
if sessionData[player] == nil then
repeat
wait(0.5)
until sessionData[player] ~= nil
end
return sessionData[player]
end
uploadDataRE.OnServerEvent:connect(function(player, statName, changeValue)
sessionData[player][statName] = changeValue
-- for _, player in pairs(game.Players:GetPlayers()) do
-- dataUpdatedRF:InvokeClient(player)
-- end
dataUpdatedRE:FireClient(player)
--dataUpdatedRF:InvokeClient(player) --only have to notify the player because he is the only one that cares?
end)
function sendDataRF.OnServerInvoke(player, statName, changeValue)
sessionData[player][statName] = changeValue
-- for _, player in pairs(game.Players:GetPlayers()) do
-- dataUpdatedRF:InvokeClient(player)
-- end
dataUpdatedRE:FireClient(player)
--dataUpdatedRF:InvokeClient(player) --only have to notify the player because he is the only one that cares?
end
-- Function the other scripts in our game can call to change a player's stats. This
-- function is stored in the returned table so external scripts can use it.
function PlayerStatManager:ChangeStat(player, statName, changeValue)
sessionData[player][statName] = changeValue--sessionData[player][statName] + changeValue
end
-- Function to retry the passed in function several times. If the passed in function
-- is unable to be run then this function returns false and creates an error.
local function dataStoreRetry(dataStoreFunction)
local tries = 0
local success = true
local data = nil
repeat
tries = tries + 1
success = pcall(function() data = dataStoreFunction() end)
if not success then wait(1) end
until tries == DATASTORE_RETRIES or success
if not success then
error('Could not access DataStore! Warn players that their data might not get saved!')
end
return success, data
end
-- Function to retrieve player's data from the DataStore.
local function getPlayerData(player)
return dataStoreRetry(function()
return playerData:GetAsync(player.UserId)
end)
end
-- Function to save player's data to the DataStore.
local function savePlayerData(player)
return dataStoreRetry(function()
return playerData:SetAsync(player.UserId, sessionData[player])
end)
end
-- Function to add player to the sessionData table. First check if the player has
-- data in the DataStore. If so, we'll use that. If not, we'll add the player to
-- the DataStore.
local function setupPlayerData(player)
local success, data = getPlayerData(player)
if not success then
sessionData[player] = false
else
if not data then --Base stats
sessionData[player] = {
AccountCoins = 0,
AccountFame = 0,
}
savePlayerData(player)
else
sessionData[player] = data
end
end
-- player.PlayerGui.StartGameGui.Frame.StartScreen.Play.Visible = true
end
-- Function to run in the background to periodically save player's data.
local function autosave()
while wait(AUTOSAVE_INTERVAL) do
for player, data in pairs(sessionData) do
savePlayerData(player)
end
end
end
-- Bind setupPlayerData to PlayerAdded to call it when player joins.
game.Players.PlayerAdded:connect(setupPlayerData)
-- Call savePlayerData on PlayerRemoving to save player data when they leave.
-- Also delete the player from the sessionData, as the player isn't in-game anymore.
game.Players.PlayerRemoving:connect(function(player)
savePlayerData(player)
sessionData[player] = nil
end)
-- Start running autosave function in the background.
spawn(autosave)
-- Return the PlayerStatManager table to external scripts can access it.
return PlayerStatManager
This is the saving script I am using. It comes from the roblox tutorial article http://wiki.roblox.com/index.php?title=Saving_Player_Data
Usually the saving works just fine. However, recently the saving has stopped working and results in this error
15:24:16.204 - Disconnect from 127.0.0.1|65009
Player -1 leaving
15:24:16.304 - An error occurred
15:24:16.305 - Stack Begin
15:24:16.305 - Script 'https://www.roblox.com/game/gameserver.ashx', Line 126
15:24:16.305 - Stack End
You can actually go to http://www.roblox.com/game/gameserver.ashx(you have to copy and paste it into the url bar for some reason) and see the code, but what can I do to fix it?
Edit 2: So I counted to line 126 (counting the spaces as lines) and got to this line
if player.Teleported then isTeleportingOut = "True" end
What?? Furthermore, the error only is on one of my computers. The same file works perfectly on the second computer.
