Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Data Not saving quickly enough?

Inside of my game i have a datastore and it keeps dropping requests is there anyway to fix this? this is the datastore.

local Datastore = game:GetService("DataStoreService")
local CaseBuxStorage = Datastore:GetDataStore("CaseBuxData")
local RAPStorage = Datastore:GetDataStore("RAPData")

game.Players.PlayerAdded:Connect(function(Player)
	local Data = Instance.new("Folder", Player)
	Data.Name = "leaderstats"
	local CaseBux = Instance.new("IntValue", Data)
	CaseBux.Name = "CaseBux"
	local RAP = Instance.new("IntValue", Data)
	RAP.Name = "RAP"
	
	local CurrentCaseBux = CaseBuxStorage:GetAsync(Player.UserId)
	print(CurrentCaseBux)
	
	local CurrentRAP = RAPStorage:GetAsync(Player.UserId)
	print(CurrentRAP)
	
	if CurrentCaseBux == nil then
		CurrentCaseBux = 0
	end
	
	if CurrentRAP == nil then
		CurrentRAP = 0
	end
	
	CaseBux.Value = CurrentCaseBux
	
	RAP.Value = CurrentRAP
	
	CaseBux.Changed:Connect(function(NewValue)
		CaseBuxStorage:SetAsync(Player.UserId, NewValue)
	end)
	
	RAP.Changed:Connect(function(NewValue)
		RAPStorage:SetAsync(Player.UserId, NewValue)
	end)
end)

2 answers

Perhaps you're saving too often? If you store all of the data for the current players in a table you can periodically loop through the data and save it for all players (like auto-saving). You could also save their data when they leave the game and on :BindToClose().

This guid is useful

Example of :BindToClose()

local currentsession = {}

local function savePlayerData(ID)
	if currentsession[ID] then --if they are in the session log (which they should be)
		local tries = 0
		local success
		repeat
			tries = tries + 1
			success = pcall(function()
				playerData:SetAsync(ID, HTTPService:JSONEncode(currentsession[ID])) --try to save the data
				print(ID .. " data has been saved")
			end)
			if not success then wait(1) end
		until tries == 3 or success -- tries 3 times or until success
		if not success then
			warn("Cannot save data for players!") -- if save fails, error
		end
	end
end

game:BindToClose(function()
	for ID, data in pairs(currentsession) do
		savePlayerData(ID)
	end
end)

3 comments

0

could you give me an example of using :BindToClose()?

0

I've edited it into my post so its readable

0

okay thank you!

Here's a quick and easy way to only sava the data once a player leaves the game. Hope it helps!

local Datastore = game:GetService("DataStoreService")
local CaseBuxStorage = Datastore:GetDataStore("CaseBuxData")
local RAPStorage = Datastore:GetDataStore("RAPData")

game.Players.PlayerAdded:Connect(function(Player)
    local Data = Instance.new("Folder", Player)
    Data.Name = "leaderstats"
    local CaseBux = Instance.new("IntValue", Data)
    CaseBux.Name = "CaseBux"
    local RAP = Instance.new("IntValue", Data)
    RAP.Name = "RAP"

    local CurrentCaseBux = CaseBuxStorage:GetAsync(Player.UserId)
    print(CurrentCaseBux)

    local CurrentRAP = RAPStorage:GetAsync(Player.UserId)
    print(CurrentRAP)

    if CurrentCaseBux == nil then
        CurrentCaseBux = 0
    end

    if CurrentRAP == nil then
        CurrentRAP = 0
    end

    CaseBux.Value = CurrentCaseBux

    RAP.Value = CurrentRAP
end)

game.Players.PlayerRemoving:Connect(function(player)
	CaseBuxStorage:SetAsync(player.UserId, player.leaderstats.CaseBux.Value)
	RAPStorage:SetAsync(player.UserId,player.leaderstats.RAP.Value)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.