I have a datastore script that basically saves the position of objects on a grid. I would like to check if there is a duration one would have to wait before stopping play on studio? When I leave after joining immediately, the positions are not saved but if I wait a while, it saves as intended?
You need a variable when loading the script. Right at the top of the loading function:
--this goes right at the top of your loading function
local failed = Instance.new("BoolValue", player)
failed.Name = "DataLoadingFailed"
failed.Value = true
--then, when you're loading data:
local data
local success, err = pcall(function()
data = myDataStore:GetAsync(key)
end)
if success then
--set data
failed.Value = false --this is the last line of the data setting
print("Loaded data successfully.")
elseif not success and err then
warn("Failed to load data for @"..player.Name)
warn(err)
player:Kick("Data loading failed")
end
You should then check inside of your saving function to see if data loaded correctly.
local function save(player:Player)
if player.DataLoadingFailed.Value == true then
print("Didn't save @"..player.Name.."'s data; data loading failed or didn't finish.")
return
end
--the rest of the saving code here
This basically checks whether the player’s data loaded fully when they joined. If it didn’t, it won’t save the new empty data. This is useful if the DataStore failed as well, because you can kick the player without their new, empty data saving.
Thank you. I already have a pcall. I’m using this instead although I’m not sure if this actually makes a difference in terms of causing the issue:
local function reqbudgetwait(req)
local currentbudget = DataStoreService:GetRequestBudgetForRequestType(req)
while currentbudget < 1 do
local currentbudget = DataStoreService:GetRequestBudgetForRequestType(req)
wait(2)
end
end
repeat
reqbudgetwait(Enum.DataStoreRequestType.GetAsync)
success2, pdata2 = pcall(database2.GetAsync, database2, player.UserId )
until success or not player:FindFirstChild(name)
I’ll include a kicking of the player if the data isn’t loaded as you’ve suggested.
I’m not really understanding the code you’ve sent… but then again, I only really use SetAsync() and GetAsync() in the DataStoreService.
But I’ve tried the system of making a failsafe within the player in my own game and it works perfectly.
It’s basically to manage the budget for DataStore to prevent errors when it reaches the limit. I felt that it was the problem because of the while loop, coupled with the wait time which affects data saving. Fail safe-wise, are you referring to your first reply?
Yeah, I was referring to the first reply. In terms of limits, are you talking about character limits or request limits? I’m pretty sure both can be managed by saving a table to the store.
I’d say its request limits? At most the pdata would end up with 6 key values because the objects being saved are only positions for 6 different grid positions. How would I apply it then?
I’m pretty sure the DataStore has a 50 character limit - if you don’t exceed that limit you should be fine. Request limits shouldn’t really be a problem as long as you don’t send too many requests at once.