Help With Datastore

Hello, I was following the tutorial posted by alvinblox on yotube, and I got an error

And So I had this error,

Here is the code,

local DataStoreService = game:GetService("DataStoreService")
local MyDSS = DataStoreService:GetDataStore("MyDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	local data
	local success, errorMessage = pcall(function()
		data = MyDSS:GetAsync(Player.UserId.."-Kills")
	end)
	if success then
		Player.leaderstats.Kills.Value = data
		Player.leaderstats.Deaths.Value = data
	else
		print("There was an error whilst getting your data!")
		warn(errorMessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	
	local success, errorMessage = pcall(function()
	    MyDSS:SetAsync(Player.UserId.."-Kills",Player.leaderstats.Kills.Value, "-Deaths", Player.leaderstats.Deaths.Value)
	end)
	if success then
		print("Data Has Been Succcessfully Saved!")
	else
		print("There was an error when saving data!")
		warn(errorMessage)
	end	
	
end)

Please Help Me,
Your Fellow Developer,
The Spikey Man

Try removing the two last variables.

MyDSS:SetAsync(Player.UserId.."-Kills",Player.leaderstats.Kills.Value)`

Hmm, But i also want to save the Deaths Value bro

I don’t believe you have used SetAsync correctly.

The first thing before a comma (,) should be the key. The next part that follows is what you want to save, if that makes sense.

MyDSS:SetAsync(Player.UserId.."-Kills",Player.leaderstats.Kills.Value)
MyDSS:SetAsync(Player.UserId.."-Deaths",Player.leaderstats.Deaths.Value)

lemme try what u say. if it works then good :slight_smile:

Wait you have another problem.

You can still do that. You can use a different key, or you can keep the same key. However, you can’t use commas to separate them, you would have to put quotation marks around the comma to separate them.

You used the same key for death and kills

local DataStoreService = game:GetService("DataStoreService")
local MyDSS = DataStoreService:GetDataStore("MyDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	local data, data2
	local success, errorMessage = pcall(function()
		data = MyDSS:GetAsync(Player.UserId.."-Kills")
        data2 = MyDSS:GetAsync(Player.UserId.."-Deaths")
	end)
	if success then
		Player.leaderstats.Kills.Value = data
		Player.leaderstats.Deaths.Value = data2
	else
		print("There was an error whilst getting your data!")
		warn(errorMessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	
	local success, errorMessage = pcall(function()
	    MyDSS:SetAsync(Player.UserId.."-Kills",Player.leaderstats.Kills.Value)
        MyDSS:SetAsync(Player.UserId.."-Deaths",Player.leaderstats.Deaths.Value)
	end)
	if success then
		print("Data Has Been Succcessfully Saved!")
	else
		print("There was an error when saving data!")
		warn(errorMessage)
	end	
	
end)

i tried ur script, dosen’t work. didn’t give any errors too

ayo but it did say this. when i changed the values and stop the game.

  16:08:04.365  Disconnect from ::ffff:127.0.0.1|58587  -  Studio
  16:08:05.446  Data Has Been Succcessfully Saved!  -  Server

Can you show me a video of you changing the values, it’s possible you’re changing them the wrong way.

Okay it started to work when i started to record. lol

Have you tried loading the data?

everyone thanks, for the help!
Datastore is really annoying for me.
Byeee

You can save a dictionary…

local playerdata = {
   Kills = Player.leaderstats.Kills.Value,
   Deaths = Player.leaderstats.Deaths.Value
}

MyDSS:SetAsync(Player.UserId .. "-Data",  playerdata)

So you don’t need any redundant setasync and getasync calls

I know, just trying not to complicate things.

I would highly recommend not using tutorials for DataStores, as many people explain them very poorly, and people end up with poorly scripted DataStores.

Also, if youre going to be saving multiple values to the same DataStore, do not use different keys.
Instead, use a table (A list, to make it simple) of the values youre saving.

The reason you’ll want to do this, and not just different save keys is because DataStores have limits on them, sending too many requests to save data will cause them to error, and not save the data.

Anyway, this should fix the problem:

local DataStoreService = game:GetService("DataStoreService")
local MyDSS = DataStoreService:GetDataStore("MyDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	local data
	local success, errorMessage = pcall(function()
		data = MyDSS:GetAsync(Player.UserId)
	end)
	
	-- You'll need to create the values if they dont already exist.
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = 'leaderstats'
	local Kills = Instance.new("IntValue", Leaderstats)
	Kills.Name = 'Kills'
	local Deaths = Instance.new("IntValue", Leaderstats)
	Deaths.Name = 'Deaths'
	-- If you have any other scripts creating these values, delete those scripts.
	
	if success and data ~= nil then
		Kills.Value = data.Kills
		Deaths.Value = data.Deaths
	elseif success and data == nil then
		print('No saved data was found')
	else
		print("There was an error whilst getting your data!")
		warn(errorMessage)
	end
end)

function SaveData(Player)
	-- Create a table to save all your values to
	local DataToSave = {}
	
	-- Look through the leaderstats folder, and insert the values into the table that just got created
	for i, v in pairs(Player.leaderstats:GetChildren()) do
		DataToSave[v.Name] = v.Value
	end
	
	-- Finally, save to the DataStore.
	MyDSS:SetAsync(Player.UserId, DataToSave)
end

game.Players.PlayerRemoving:Connect(SaveData)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.