Hello all scripters, i have encountered an unknown problem in my game that results in unreliable datastores.

One time it saved my stats as my friend who was playing the game. Sometimes it just doesnt even update, so when i join in again all the stats havent even changed from last time. But then sometimes it works fine??

here is the hefty datastore script i created.

--Variables
local leveldata = game:GetService("DataStoreService"):GetDataStore("level")
local EXPdata = game:GetService("DataStoreService"):GetDataStore("EXP")
local xpLeveldata = game:GetService("DataStoreService"):GetDataStore("xpLevel")
local Golddata = game:GetService("DataStoreService"):GetDataStore("Gold")
local toolsdata = game:GetService("DataStoreService"):GetDataStore("Tools")
local HttpService = game:GetService("HttpService")


--Data Storage Save
function savedata(dataname, playerid, value)
	game:GetService("DataStoreService"):GetDataStore(dataname):SetAsync(playerid, value)
end

--Leaderboard Variables
game.Players.PlayerAdded:Connect(function(player)
	playerKey = "user."..player.UserId	-- user key
	print(playerKey)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	--Gold
	local gold = Instance.new("IntValue")
	gold.Name = "Gold"
	gold.Value = Golddata:GetAsync(playerKey) or 0
	gold.Parent = leaderstats

	
	--Level
	local level = Instance.new("IntValue")
	level.Value = leveldata:GetAsync(playerKey) or 1
	level.Name = "Level"
	level.Parent = leaderstats

	
	--XP
	local EXP = Instance.new("IntValue")
	EXP.Value = EXPdata:GetAsync(playerKey) or 0
	EXP.Parent = player
	EXP.Name = "Exp"
	
	--XP Needed
	local xpLevel = Instance.new("IntValue")
	xpLevel.Value = xpLeveldata:GetAsync(playerKey) or 84
	xpLevel.Name = 'ExpNeeded'
	xpLevel.Parent = player
	
	--tools
	local tools = toolsdata:GetAsync(playerKey)
	if tools then
		for i,v in pairs(tools) do
			local tool = game.ReplicatedStorage.Tools:FindFirstChild(v)
			if tool then 
				tool:Clone().Parent = player.Backpack
				tool:Clone().Parent = player.StarterGear					
			end
		end
	end

game.Players.PlayerRemoving:Connect(function(player) -- save player stats
	local toolsToSave = {}		
	for i,v in pairs(player.StarterGear:GetChildren()) do
		if v then
			table.insert(toolsToSave,v.Name)
		end
	end
	game:GetService("DataStoreService"):GetDataStore('Tools'):SetAsync(playerKey, toolsToSave)
	savedata('level',playerKey,player.leaderstats.Level.Value)
	savedata('EXP',playerKey,player.Exp.Value)
	savedata('xpLevel',playerKey,player.ExpNeeded.Value)
	savedata('Gold',playerKey,player.leaderstats.Gold.Value)
	print('data saved')
end)

if anyone could take a look and make any suggestions i would greatly appreciate it! thank you all!

4 comments

0

Try storing it all in one table, use a pcall, and use BindToClose so you can do the stuff before the game closes

0

hmm, do you know how i would get all of this into a table? ive been trying to think of a way.

0

Storing it all in one table is a terrible idea for perma storage, unless you mean storing it all in one table as some mega table and saving it to datastores.

0

well now im unsure of what would be more efficient for my game :\ should i not use tables then?