Does this prevention script against a data loading problem work?

  1. 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
1 Like

I believe this does work; however, I would recommend changing the wait(1) into task.wait(7).

2 Likes

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

okay

so, this line removes 1 attempt from the number if attempts left

attempts = attempts - 1

this line will repeat the code until all 10 attempts have been used

until attempts <= 0

if the data does not load within the 10 attempts, then

if not SettingDataLoaded.Value then

kick the player

plr:Kick("Data not loaded, please rejoin later")
if you need to know more just let me know
1 Like

Thank you for taking the time to explain to me.

So if I understand correctly when the script gets to

attempts = attempts - 1

then the function

local success, err = pcall(function()

is repeated 10 times, and it is repeated thanks to the

repeat

above the function ?

However I do not understand why at this time, the function repeats since the “repeat” is already passed?

And the

if not SettingDataLoaded.Value then

I don’t understand, because there is a SettingDataLoaded.Value which is created above? So there is a value, whether true or false no ?

if not SettingDataLoaded.Value == true then

Would be better no ?

Thank you !

(Sorry for these questions, I’m still a beginner.)

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)
2 Likes

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

1 Like

it will repeat the code until the number of attempts = 0 or if the data loads in this code

1 Like

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

You should use Profile Service. Trust me, going around using one of these data store modules doesn’t work at all.

1 Like

Profile Service is one of the Safest Datastore you can add, games like Bedwars on Roblox uses it.
You should do like what @RefusalMan have said.

1 Like