Datastore Tutorial for Beginners

I placed the vanilla script at the beginning of your post inside of a script named DataSave inside a folder named Framework in ServerScriptService and it doesn’t seem to show a leaderstat on the leaderboard.
code


-- // Assigning variables //
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore") -- This can be changed to whatever you want

local function saveData(player) -- The functions that saves data
	
	local tableToSave = {
		player.leaderstats.Money.Value; -- First value from the table
		player.leaderstats.Coins.Value -- Second value from the table
	}
	
	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)
	
	if success then -- If the data has been saved
		print("Data has been saved!")
	else -- Else if the save failed
		print("Data hasn't been saved!")
		warn(err)		
	end
end

end

game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game
	
	-- // Assigning player stats //
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Parent = leaderstats
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	
	local data -- We will define the data here so we can use it later, this data is the table we saved
	local success, err = pcall(function()
		
		data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore
		
	end)
	
	if success then -- If there were no errors and player loaded the data
		
		Money.Value = data[1] -- Set the money to the first value of the table (data)
		Coins.Value = data[2] -- Set the coins to the second value of the table (data)
		
	else -- The player didn't load in the data, and probably is a new player
		print("The player has no data!") -- The default will be set to 0
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
	local success, err  = pcall(function()
		saveData(player) -- Save the data
	end)
	
	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
	end
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
		local success, err  = pcall(function()
			saveData(player) -- Save the data
		end)
		
		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)

And I have too much JUNK in my output to show you what’s there.

Thanks,
d_cyph

3 Likes

Make sure where you put the script isn’t getting blocked or anything

for some reason, i tried using your method to save data on my game, but no matter what it always errors when i change the name of the datastore, any way to fix this? image

2 Likes

Post lines 40-60 to so we can evaluate what’s wrong with the code.

EDIT: Never mind, I have the same problem and I’m currently debugging it.
Screenshot 2020-10-22 154743

EDIT 2: I fixed it.
Screenshot 2020-10-22 155055


Add if [currencyName] then to check if your currency loads under the if success then.

Hope this helps! :slight_smile:

EDIT 3: Realized I spelled “previously” wrong on line 461 in the second picture LOL.

3 Likes

It actually worked, thanks alot my guy :+1:

1 Like

I think it’s not safe. If something goes wrong when fetching player’s data, all data will be lost.
Probably this is better:

local success, result = pcall(function()
	local data = MoneySaves:GetAsync(player.UserId)
	return data
end)

if success then
	-- nothing went wrong	

	if result then -- if everything goes well, "result" will be the player's data (if nil, player has no data)
		Money.Value = result[1]
	else
		print('Player has no data.')
	end
else
	warn(result) -- if it fails, "result" will be the error message
	player:Kick('Avoiding dataloss.')
end

It is also a good idea to check whether the player’s data has been loaded before saving it.

I’m new with Datastore, so I’m not sure if this is right.

5 Likes

I wasn’t trying to fix the data loss problems, just the one error. :slight_smile:

1 Like

Ok then :slight_smile:
Maybe this will be important for other people.

5 Likes

i actually did that with my line of code to prevent data loss, but yeah it might be useful for other people

if not success then
	player:Kick("Couldnt find data, kicking player to prevent data loss")
elseif success then
	
	if data then
		Strength.Value = data[1]
		Speed.Value = data[2]
		InCombat.Value = data[3]
		SkinColor.Value = data[4]
		warn(player.Name.."'s data has been loaded.. ID"..player.userId)
		
	else
		warn(player.Name.." doesnt have any data, assigning default values.. ID"..player.userId)
		InCombat.Value = false
		Speed.Value = 16
		Strength.Value = 1
		SkinColor.Value = SkinColors[math.random(#SkinColors)]
		print(SkinColor.Value)
	end

	
end
4 Likes

Amazing tutorial! It helped me understand how to actually save data! Before it just made my brain want to explode. I did find one error with the code.

You do need to delete that last end there. It’s extra and throws an error:

2 Likes

Awesome tutorial! Really appreciate it and really helped me for saving data in my game, now I understand it perfectly! Thanks so much for the help and keep up the great work! :smiley: :+1:

1 Like

I really like the tutorial because it includes game:BindToClose() function and it is easy to understand. Good job!

2 Likes

Thanks!

Also thanks for showing me the error, I fixed it right away!

No problem dude!

Keep going and making your game, and I hope someday I might be able to see it!

Yeah thanks for liking it, it took me a while to make the tutorial as best as possible!

Awesome!

I know DataStoreService very well but this is an excellent tutorial for beginners!

I will be sharing this!

1 Like

Really great tutorial! Is there anyway you can use it if you’ve already creating the leaderstats in another script?

Sorry if it’s a dumb question lol, I’m new to DataStores

2 Likes

Is there a way that i can load in tools?

2 Likes

I’d like to see an advanced tutorial soon or it might already exist and my internet isn’t catching up with that. Great beginner tutorial :+1:

1 Like

Sorry for the late reply, i haven’t been active as much if you already made 1 data store, look at the top of the script you’ve made and look at your datastore name, then you can use that datastore name in the new script u can make using this tutorial,

Cheers!