Datastore Tutorial for Beginners

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!

2 Likes

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!

There are other tutorials regarding datastores with tools

I tried this and it kept outputting the error.

-- // 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
	}

	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

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 Playtime = Instance.new("IntValue")
	Playtime.Name = "Playtime"
	Playtime.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

		Playtime.Value = data[1] -- Set the money to the first 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)

By error, I mean the output says data has not been saved.


That is every time I play, on studio, and on roblox…

I do have API services enabled…

2 Likes

Simply print the error to allow you to debug it.

Sorry I don’t know what you mean. Could you explain that

The second value returned by a pcall is the error message or the value returned by the function it ran. And so, you could do this:

local Success, Var = pcall(function()
    table.sort(nil) -- Would throw an error
end)

if not Success and Var then
    error(Var)
end

@DevHelpAccount , no. You need to edit the script, let me just do it for you:

-- // 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
	}

	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

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 Playtime = Instance.new("IntValue")
	Playtime.Name = "Playtime"
	Playtime.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

		Playtime.Value = data[1] -- Set the money to the first 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!")
            error(err)
		end
	end
end)

Do I just run that in the command bar?

It says that Money is not a valid member of my player. I use Playtime on mine

Sorry for late response, just change Money with Playtime in the script

1 Like

Don’t worry! I have it working! Thanks anyway! :grin:

Edit: Oh, and not “anyway” just thanks for this amazing datastore tutorial! Recommend it!

:grin:

For some reason, I learnt more about I, v in pairs and tables then I did for datastores :grin:

1 Like

I made this mistake too, At the top in the local tabelToSave it says player.leaderstats. Money . value, the mistake you made is you re-named the Money to “Playtime”, all you have to is change player.leaderstats. Money . value to: player.leaderstats.Playtime.value and then it should be fixed.
Hope this helps! :coefficients:

I have a question about that, and is that how it would prevent the loss of data? It is simply expelling it, and when it enters again, what will happen?