✓ Accepted answer
You're checking for the wrong success variable. you are in scope of the first suc variable meaning you are checking if the first success variable is true, which, if you error it will never be and you'll be stuck in an infinite loop forever. You instead want to be checking if the second success variable is true/false.
another thing that might end up looping forever is if we can't save the data. set a maximum amount of tries like in the example below so that doesn't become an issue.
Another thing, is you are asking the Datastore Service to use SetAsync() where you want to have the exampleDataStore use SetAync()
We also want to save the player's data, not an empty table, so I included a place to store all of the player's in game's data so you can access it when you want to save to datastore.
Not required, but you can use a module script so you can save / get the data from any script.
lastly, i'd suggest u using updateasync() over setasync() as setasync might not be the most recent data causing data loss.
#PLEASE, PLEASE pLEaSE consider DOING THIS IF YOU DONT WANT TO LOSE DATA
i wrote about update async here
and i got the idea of using update async like that from here:
(in english, AllPlayerData AT playerNameGoesHere)
AllPlayerData = {
[playerNameGoesHere] = {}
}
local DSS = game:GetService("DataStoreService")
local exampleDataStore = DSS:GetDataStore("Example1")
local function saveData(player)
local success = nil
success , err = pcall(function()
exampleDataStore:SetAsync(player.UserId, AllPlayerData[player.Name])
end)
if not success then
local attempts = 0
print(err)
while(not success or attempts < 3)do
wait(3)
success, err = pcall(function()
exampleDataStore:SetAsync(player.UserId, AllPlayerData[player.Name])
end)
attempts = attempts + 1
end
end
end
9 comments
@royaltoe thanks, I just roughly wrote it since I'm in school so I want to check if it's not successful? Also why do we have a attempt I know it's not to spam requests but do we just let it be if it fails?
sure thing. we have an attempt because if for some reason, setasync() isn't working, we want to give up at some point. we don't want the loop to get stuck in an infinite loop trying to save the data when it can't. this might happen if roblox's setasync() isn't working (for whatever reason) or if the setasync() fails because our player variable is nil (aka we didn't pass in the player in our functi
@royaltoe what would be a recommended amount of attempts?
@royaltoe also sorry to bother you but I do have an unsolved problem wondering if you could check it out:
https://scriptinghelpers.org/questions/41770/best-way-to-constantly-update-orientation-cframe-of-character-from-the-client-to-the-server
if you want to do it that way, maybe 3-5 attempts with an attempt every 30 seconds-1 min? it's up to you really. i did something similar in my popular game and didn't have many issues with datastore. can't quite remember what i did and don't wanna bother going through my code, but that sounds fine to me.
rn i just use UpdateAsync() and save the player's data every 5 mins + save when they leave the game/BindToClose() but should be able to do both the way you want to do it in your code and autosave every 5 mins if youd like
@royaltoe thanks! Could you check out my other unsolved problem :3
yea ill take a look and woah that is an ancient post