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

DataStore only occasionally saving/retrieving data?

So, I asked a question a day or two ago, about my DataStore not working. Well, it works now, but only occasionally.

I'm not sure what to do, because I don't wanna completely break it again, so I came here. If anyone can help, I'm new to scripting so it would be appreciated. Here's my code:

local dataStoreService = game:GetService("DataStoreService")
local StatStore = dataStoreService:GetDataStore("DataStats")

game.Players.PlayerAdded:Connect(function(player)
	game.Workspace:WaitForChild(player.Name)
	player:WaitForChild("leaderstats")
	player.PlayerGui.Codes.SpeedBoost.Parent = player
	
	local Key = player.UserId
	
	local Leaderstats = player.leaderstats
	local Steps = Leaderstats.Steps
	local SpeedBoost = player.SpeedBoost
	
	local DefaultSteps = 0
	
	local DefaultData = {
		0,
		false
	}
	
	local Stats = {
		Steps.Value,
		SpeedBoost.Value
	}
	
	local NewStats
	local recieved, notRecieved = pcall(function()
		NewStats = StatStore:GetAsync(Key)
	end)
	
	if recieved then
		Steps.Value = NewStats[1]
		SpeedBoost.Value = NewStats[2]
		print("Stats were recieved")
		wait(1)
		StatStore:SetAsync(Key, Stats)
	else
		StatStore:SetAsync(Key, DefaultData)
		print("Stats were not recieved")
		warn(notRecieved)
	end
	
	wait(0.1)
	
	while wait(10) do
		local saved, notSaved = pcall(function()
			StatStore:UpdateAsync(Key, function(oldValue)
				return Stats
			end)
		end)
		
		if saved then
			print("Stats were auto saved!")
		else
			print("Stats didn't auto save.")
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Key = player.UserId
	
	local Leaderstats = player.leaderstats
	local Steps = Leaderstats.Steps
	local SpeedBoost = player.SpeedBoost
	
	local DefaultData = {
		0,
		false
	}
	
	local Stats = {
		Steps.Value,
		SpeedBoost.Value
	}
	
	local success, result = pcall(function()
    	StatStore:UpdateAsync(Key, function(oldValue)
			return Stats
    	end)
	end)
	
	if success then
		print("Stats were saved!")
	else
		print("Stats were not saved.")
		warn(result)
	end
end)

Sorry if it's messy, like I said, I'm new to scripting.

Steps is an IntValue and SpeedBoost is a BoolValue.

1 answer

✓ Accepted answer

It seems like you're updating it too often, which might exhaust the datastore, so I'd recommend changing the update interval to at least 60 seconds.

2 comments

0

I knew it was too often, I only had it at 10 for testing purposes. But I'll try changing it anyway, thanks.

0

It seems to work every time now, I can't believe I didn't think to change that. Thanks again!

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