This is my first datastore, and its not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    datastore that saves
  2. What is the issue? Include screenshots / videos if possible!
    I really know
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried leaving using esc L enter and I tried outside of studio
local DS = game:GetService("DataStoreService")
local fortunes = DS:GetDataStore("Fortunes") -- Creates (or gets) a data store


game.Players.PlayerAdded:Connect(function(player) -- gives player leaderboare as soon as join	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"                     -- REQUIRED to name it leaderstats
	
	local times = Instance.new("IntValue", leaderstats)  -- stats in leaderboard
	times.Name = "Fortunes Told"
	
	local id = "Player_"..player.UserId -- save id
	
	local _data -- setting data to nothing so scope doesnt ruin thing
	
	local success, errormessage = pcall(function() -- do this all the time
		_data = fortunes:GetAsync(id)    -- I think it gets data 
	end)
	
	if success then 
		times.Value = _data
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Id = "Player_"..player.UserId
	local data = player.leaderstats["Fortunes Told"].Value
	
	local success, errormessage = pcall(function()
		fortunes:GetAsync(Id, data) -- saves Fortune Value when leave
	end)
	if success then
		print("Data successfully saved!")
	else
		print("There was an error!")
		warn(errormessage)
	end
end)

For the PlayerRemoving instead of using SetAsync you used GetAsync

3 Likes

Try playing it in a real game. Play testing in studio is buggy.

You never actually add anything to the DataStore so the GetAsync’s aren’t working since there’s nothing in it.

1 Like

Do what @NotZylon , should fix it. I just want to give you a few tips on data stores that I wish I knew when I started to make datastores. dont save different values in different data stores. Since it’ll cause lag. Instead use tables. Also use :UpdateAsync instead of :SetAsync. Since :SetAsync can cause data losses, while :UpdateAsync is less likely to fail(almost impossible) So do something like this:

local DS = game:GetService("DataStoreService")
local GDS = DS:GetDataStore("GlobalDataStore") -- Creates (or gets) a data store


game.Players.PlayerAdded:Connect(function(player) -- gives player leaderboare as soon as join	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"                     -- REQUIRED to name it leaderstats

	local times = Instance.new("IntValue", leaderstats)  -- stats in leaderboard
	times.Name = "Fortunes Told"

	local id = "Player_"..player.UserId -- save id

	local _data -- setting data to nothing so scope doesnt ruin thing

	local success, errormessage = pcall(function() -- do this all the time
		_data = GDS:GetAsync(id)    -- I think it gets data 
	end)

	if success then 
		times.Value = _data.Leaderstats.Fortunes -- loads our fortunes data.
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Id = "Player_"..player.UserId
	local data = {
		["Leaderstats"] = {
			["Fortunes"] = player.leaderstats["Fortunes Told"].Value
		}
	}

	local success, errormessage = pcall(function()
		GDS:UpdateAsync(Id, function()-- this is to prevent data losses. As Update async is better for saving data. As Set async can lose data.
			return data -- we're returning the data we want to save.
		end)	
	end)
	if success then
		print("Data successfully saved!")
	else
		print("There was an error!")
		warn(errormessage)
	end
end)