What do you want to achieve?
Hello, I am trying to secure the data of my game in case a player joins and his data is not loaded. I found the following script which was written by @4667hp i guess. I’m having trouble knowing if it works. Can you tell me ? Thanks
Players.PlayerAdded:Connect(function(plr)
local SettingDataLoaded = Instance.new('BoolValue',plr)
SettingDataLoaded.Name = "SettingDataLoaded"
SettingDataLoaded.Value = false
local data
local attempts = 10
repeat
local success, err = pcall(function()
data = SaveData:GetAsync(plr.UserId) or {}
end)
if success then
print("success")
SettingDataLoaded.Value = true
break
else
warn("A problem is detected with the data, attempts : 10")
end
wait(1)
attempts = attempts - 1
until attempts <= 0
if not SettingDataLoaded.Value then
plr:Kick("Data not loaded, please rejoin later")
end
Hi, no errors in the output. Only I have trouble understanding the second part of the script
from here
else
warn("A problem is detected with the data, attempts : 10")
end
wait(1)
attempts = attempts - 1
until attempts <= 0
if not SettingDataLoaded.Value then
plr:Kick("Data not loaded, please rejoin later")
end
Anytime extraneous polling is involved in code you’re bound to be doing things the most inefficient way
:GetAsync is an asynchronous, yielding function meaning all code in the thread it was called in will wait until :GetAsync either returns something or errors. Brute forcing calls to it will not make it more reliable and it just makes your code messier
It may be a bit too advanced for you, but there’s this awesome Promise Library that implements JavaScript-like promises so you can handle asynchronous functions better. Here’s some pseudocode on how you would implement it:
function GetPlayerData(Player: Player)
return Promise.new(function(Resolve, Reject)
local Data, Error = DataStore:GetAsync(Player.UserId)
if Data then
return Resolve(Data)
else
return Reject(Error)
end
end)
end
game:GetService("Players").PlayerAdded:Connect(function(Player: Player)
GetPlayerData(Player):andThen(function(Data)
end):catch(warn)
end)
With a print(“hi”) just below the repeat i can know that the function is well repeated after a volontary fail. I didn’t know repeat was to do things like this
Thank you for your reply. I had never seen a script like this for data loss prevention. I’m gonna try it. i’m gonna try this to see what happen, thanks for sharing