Is there any other way to improve my DataStore more?

development
scripting

#21

Retrying a reasonable amount of times and if it still doesn’t work, leaving them with the default data but:

  1. Telling the player their data is a backup and warn them their data won’t be saved
  2. Not saving it under any circumstance

#22

What if so I made BoolValue called DataLoaded if DataStore throws errors I won’t make the value true if it loads then I will set the value to true. When a player leaves it will check if DataLoaded value is set to true if it is set to true, then it will save the data if not then it won’t save it and the player will keep his old data. How does this sounds?


#23

That sounds fine to me.


#24

If a datastore throws error should I kick the player or make message that tells him that the data was not loaded successfully?

or send him to new lobby


#25

That’s up to you, but I would suggest just making sure the player is aware their data didn’t load with the option to server hop.


#26

So something like this?

local Load = function(folder,data,plr)
	
	local Succsess,Data = pcall(function()
		return data:GetAsync(plr.UserId);
	end)
	
	if (not Succsess) then
		print("faild to load data");
		return;
	end
	
	if (Data == nil) then
		return;
	end

	plr.DataLoaded.Value = true;
	
	for i,v in next,Data do
		print(v.name)
		if (folder:FindFirstChild(v.name)) then
			folder:FindFirstChild(v.name).Value = v.value;
		else
			print(v.name.." is not valid member of "..folder.Name);
		end
	end
end

#27

You should attempt to retry the GetAsync n times. You should also tell the players their data didn’t load.


#28

Ok, thanks for the help.


#29

I am running into an issue now If a player is new to the game and I load data it would be nil and if datastore throws error error it would also be nil how would I detect if the player is new to the game and the data is nil because of that or because the datastore did not load successfully.

local Module = {}

function Module:Load(plr,leaderstats,GDS)
	local Succsess,Data = pcall(function()
		return GDS:GetAsync(plr.UserId);
	end)

	if (Succsess and Data) then
		for i,v in next,Data do
			if (leaderstats:FindFirstChild(Data.name)) then
				leaderstats:FindFirstChild(Data.name).Value = leaderstats:FindFirstChild(Data.name) + v.value;
			else
				warn(v.name.." is not valid member of "..leaderstats.Name);
			end
		end
		plr.DataLoaded.Value = true;
	else
		plr.DataLoaded.Value = false;
	end
end

function Module:Save(leaderstats)
	local Data = {};
	for i,v in next,leaderstats:GetChildren() do
		local TempData = {
			name = v.Name;
			value = v.Value;
		};
		table.insert(Data,TempData);
	end
	return;
end

return Module;

#30

pcall returns whether the function inside errored or not. Just check that value.


#31

So like this

function Module:Load(plr,leaderstats,GDS)
	local Succsess,Data = pcall(function()
		return GDS:GetAsync(plr.UserId);
	end)
        if (Succsess and Data == nil) then
          plr.DataLoaded.Value = false;
        end
	if (Succsess and Data) then
                plr.DataLoaded.Value = true;
		for i,v in next,Data do
			if (leaderstats:FindFirstChild(Data.name)) then
				leaderstats:FindFirstChild(Data.name).Value = leaderstats:FindFirstChild(Data.name) + v.value;
			else
				warn(v.name.." is not valid member of "..leaderstats.Name);
			end
		end
	end
end

#32

No. This’ll fire if the player has no data.

Check if it didn’t succeed. If it didn’t succeed, that means it errored.