Hi all, I have this datastore script (below) which works fine (as far as I can tell) apart from the "savePlayerData" function which for some reason never succeeds...
Can anyone explain whats going wrong with this? Is like 57 ok? considering that currentsession[ID] will give a table... and looking at like 20-24 is it acceptible to save things which arent numbers? e.g. a spawn location (describing an instance... i guess)
Thanks for having a look!
local _M = {}
--Get the datastore "playerData"
local DS = game:GetService("DataStoreService")
local playerData = DS:GetDataStore(game.GameId .. "_TestPlayerData")
local StarterStats = {Stage = 0, Cash = 100, SpawnPoint = game.Workspace.SpawnLocation} -- list of default stored data (also gives data names for reference)
local currentsession = {} --Set up a table for plaeyrs in current session
local autosave_interval = 120 --Time to wait between autosaves
--[[
IMPORTANT: Call this function from other scripts when updating player data
]]--
function _M:ChangeStat(player, stat_name, value)
local ID = "Player_" .. player.UserId
assert(typeof(currentsession[ID][stat_name]) == typeof(value), "Changestat Error: Types do not match")
--[[assert is a manual way of throwing errors
if first arg == true then code will run
if first arg == false second arg (error) will run]]--
if typeof(currentsession[ID][stat_name]) == "number" then --if the value given is a numebr then
currentsession[ID][stat_name] = currentsession[ID][stat_name] + value
else -- if the value given is not a number
currentsession[ID][stat_name] = value
end
player.player_stats:FindFirstChild(stat_name).Value = currentsession[ID][stat_name] --update the player_stat folder in player
end
local function setupPlayerData(player)
local playerStatsFile = game.ReplicatedFirst.player_stats:Clone()
playerStatsFile.Parent = player
local ID = "Player_" .. player.UserId
local success, data = pcall(function()
return playerData:GetAsync(ID) --Checks if there is a datastore for the player online
end)
if success then
if data then --if there is, iport it
currentsession[ID] = data
print("DS: Data has been imported for " .. player.Name)
else --if not, set one up
currentsession[ID] = StarterStats
print("DS: New data has been created for " .. player.Name)
end
for _,key in pairs(player.player_stats:GetChildren()) do --Load the data into the player_stats folder
key.Value = currentsession[ID][key.Name]
end
else warn("Cannot access datastore for players!")
end
end
local function savePlayerData(ID)
if currentsession[ID] then --if they are in the session log (which they should be)
local tries = 0
local success
repeat
tries = tries + 1
success = pcall(function()
playerData:SetAsync(ID, currentsession[ID]) --try to save the data
end)
if not success then wait(1) end
until tries == 3 or success -- tries 3 times or until success
if not success then
warn("Cannot save data for players!") -- if save fails, error
end
end
end
local function saveOnExit(player)
local ID = "Player_" .. player.UserId
savePlayerData(ID) --pass the players ID to be saved when they exit
end
local function autosave()
while wait(autosave_interval) do
for ID, data in pairs(currentsession) do --loops through session log
savePlayerData(ID) --passes each players ID to be saved
end
end
end
spawn(autosave) -- runs autosave in the background
game.Players.PlayerAdded:Connect(setupPlayerData)
game.Players.PlayerRemoving:Connect(saveOnExit)
return _M

1 comment
Have you tried this maybe? https://scriptinghelpers.org/questions/109148/why-is-my-datastore-not-saving-data-nor-giving-any-sort-of-error#100220