How do I write this DataStore script in the best way?

local DataStoreService = game:GetService("DataStoreService")
local Event = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("SaveMyStats")

game.Players.PlayerAdded:Connect(function(player)
	local Stats = Instance.new("Configuration", player)
	Stats.Name = "Stats"
	
	-- Main stats
	
	local diamonds = Instance.new("IntValue", Stats)
	diamonds.Name = "Diamonds"
	diamonds.Value = DataStoreService:GetDataStore("DiamondDataStore"):GetAsync(player.UserId) or 0
	
	local level = Instance.new("IntValue", Stats)
	level.Name = "Level"
	level.Value = DataStoreService:GetDataStore("LevelDataStore"):GetAsync(player.UserId) or 1
	--
	-- Other stats
	local parachute	 = Instance.new("StringValue", Stats)
	parachute.Name = "Parachute"
	parachute.Value = DataStoreService:GetDataStore("ParachuteDataStore"):GetAsync(player.UserId) or "Black"
	
	local skydiveseconds = Instance.new("IntValue", Stats)
	skydiveseconds.Name = "SkydiveSeconds"
	skydiveseconds.Value = DataStoreService:GetDataStore("SkydiveSecondsDataStore"):GetAsync(player.UserId) or 0
	
	local tutorial = Instance.new("BoolValue", Stats)
	tutorial.Name = "Tutorial"
	tutorial.Value = DataStoreService:GetDataStore("TutorialDataStore"):GetAsync(player.UserId) or false
	--
end)

function SaveMyStats(player)
	-- Main stats
	if player:FindFirstChild("Stats") then
		DataStoreService:GetDataStore("DiamondDataStore"):SetAsync(player.UserId, player.Stats:FindFirstChild("Diamonds").Value)
		local level = DataStoreService:GetDataStore("LevelDataStore"):GetAsync(player.UserId)
		if level ~= nil and level <= player.Stats:FindFirstChild("Level").Value	then
			DataStoreService:GetDataStore("LevelDataStore"):SetAsync(player.UserId, player.Stats:FindFirstChild("Level").Value)
		elseif level == nil then
			DataStoreService:GetDataStore("LevelDataStore"):SetAsync(player.UserId, player.Stats:FindFirstChild("Level").Value)
		end
		--
		-- Other stats
		DataStoreService:GetDataStore("ParachuteDataStore"):SetAsync(player.UserId, player.Stats:FindFirstChild("Parachute").Value)
		DataStoreService:GetDataStore("SkydiveSecondsDataStore"):SetAsync(player.UserId, player.Stats:FindFirstChild("SkydiveSeconds").Value)
		DataStoreService:GetDataStore("TutorialDataStore"):SetAsync(player.UserId, player.Stats:FindFirstChild("Tutorial").Value)
		--
	end
end

game.Players.PlayerRemoving:Connect(SaveMyStats)
Event.OnServerEvent:Connect(SaveMyStats)

This is the script I use to store and load data in my games. It sometimes resets player data, which is really annoying. A friend of mine told me to use "pcall()"s, but I couldn’t implement it to the current script. Also, another friend of mine asked me why not put everything on one data store, I don’t know how to do that either.

If someone could help me rewrite this, that would be much appreciated!

3 Likes

Well if you know what a table is you can easily put it all on one datastore,

game:GetService("DataStoreService"):GetDataStore("Test"):SetAsync(someUserId, {
	diamonds = 0; --Haha you're poor
	level = 0; --Haha you're weak
	parachute = false; --Good luck on skydiving
	skyDiveSeconds = 0; --First time? You're goin to die
})

Then for the pcall part, if you want to get information

local data
pcall(function()
	data = game:GetService("DataStoreService"):GetDataStore("Test"):GetAsync(someUserId)
end)

Now don’t worry, pcalls aren’t like spawns where they don’t hold the script. This will wait for the request to fail or to succeed and then set the information

9 Likes

Thanks to you, I was able to add pcalls!

local success, levelVal = pcall(function() return DataStoreService:GetDataStore("LevelDataStore"):GetAsync(player.UserId) or 1 end)
if success then
	level.Value = levelVal
elseif not success then
end

What do I do if success is a false? Do I kick the player?

1 Like

If it’s false then typically it means that the data doesn’t exist, so you would create the data in that case. However, if the services are down or not responding you won’t have to worry since the saving wouldn’t work anyways (btw don’t forget to click that check icon on my response :slight_smile:

3 Likes

That uh checkbox would be nice to have btw

2 Likes