I wish to create a simple Data Store system using module scripts for learning purposes and to organise my code better. However, I have come across an issue with saving the data, everything is fine with the code so far, whereas the SetAsync pcall always returns an error in the console:
I have tried using repeat loops, I have tried changing the keys of the GetAsync and SetAsync pcalls to something more simple like the player’s UserId which were my original intentions for when they join the game. I have looked on the developer hub, I have tried everything where it is relevant to my code, I am getting to the point that I need to use bindToClose which only works when either the internet connection is being interrupted or an error has occurred in the Roblox player.
Can you guy please help me reach a conclusion on the reason why the SetAsync pcall is returning an error? Thank you for the help! I am new to posting so expect some missing details.
Hello!
I don’t know much about DataStores, but I’ll do what I can anyway.
Is it only SetAsync that fails? Have you tried playtesting this in a published game (outside of studio)? I know that saving and loading can be a little finnicky within studio.
function Data:SaveLevelDataStore()
local dataToSave = {
level = self.level,
exp = self.exp,
points = self.points
}
local attempt = 0
local success, result
repeat
attempt += 1
success, result = pcall(function()
return LevelStore:SetAsync(tostring(self.playerId), dataToSave)
end)
if success then
print('Player has saved level data successfully!')
return true
else
warn('Error saving level data on attempt', attempt, 'for player', self.playerId, result)
wait(2)
end
until attempt >= 3 or success
if not success then
warn('Failed to save level data after 3 attempts for player', self.playerId)
end
return success
end