Trying to improve datastore scripts

In my game, I have many datastore scripts.

The issue with his is it sends a lot of requests and sometimes all of the data is wiped.

I can also only save up to 7 values per datastore script.

All of the datastore scripts are formatted like this. Some have more values then the others.

local DS = game:GetService("DataStoreService"):GetDataStore("DatastoreName")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "plr"..plr.userId
	local savevalue1 = plr.Folder.Value1
	local savevalue2 =  plr.Folder.Value2
	local savevalue3 =  plr.Folder.Value3
	local savevalue4 =  plr.Folder.Value4
	local savevalue5 = plr.Folder.Value5
	local savevalue6 =  plr.Folder.Value6
	
	

	local GetSaved = DS:GetAsync(plrkey)
	if GetSaved then
		savevalue1.Value = GetSaved[1]
		savevalue2.Value = GetSaved[2]
		savevalue3.Value = GetSaved[3]
		savevalue4.Value = GetSaved[4]
		savevalue5.Value = GetSaved[5]
		savevalue6.Value = GetSaved[6]
		
	else
		local NumbersForSaving = {savevalue1.Value, savevalue2.Value, savevalue3.Value, savevalue4.Value, savevalue5.Value, savevalue6.Value}
		DS:GetAsync(plrkey, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	DS:SetAsync("id"..plr.userId, {plr.Folder.Value1.Value, plr.Folder.Value2.Value, plr.Folder.Value3.Value, plr.Folder.Value4.Value, plr.Folder.Value5.Value, plr.Folder.Value6.Value})

I want to improve 2 things:

    1. How can I send out fewer requests?
    1. How do I save more than 7 values per datastore script?

This part makes no sense. You are trying to save data via :GetAsync(). Do you mean :SetAsync()? If you do mean SetAsync why are you trying to save it when the player joins. They will have no data when they first join unless they have it in the datastore which the first part of the main part of the if statement deals with, so your just using up some of your rate limit for something you really don’t need.

You need to have your :GetAsync inside of a pcall function (also anything to do with datastores such as :SetAsync(). If an error occurs it will make the whole script not work so you need it inside of a pcall function.

There is no end) at the bottom of the function you connect to so it will very likely error unless you just forget to copy the end) on the DevForum.

Also the key you have there to set the data is different then the one you use to get the data. To get the data you have plr + userid but when saving the data you have the id + userid.

Only way is like what I said about the else in the if statement there is no need to get or save data there.

You can just add them in a table like your doing inside the code you sent. You should be able to save more then 7 values inside of a table unless I am mistaken. Unsure why your trying to save more then 7 values inside of a datastore however.

3 Likes