How to make a datastore

i made a data store but it does not save and i have api on:

local datastore = game:GetService("DataStoreService"):GetDataStore("Data")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"

	local Gold = Instance.new("NumberValue")
	Gold.Parent = leaderstats
	Gold.Name = "Gold"

	local Rebirths = Instance.new("NumberValue")
	Rebirths.Parent = leaderstats
	Rebirths.Name = "Rebirths"

	local debounce = Instance.new("BoolValue")
	debounce.Value = false
	debounce.Name = "Debounce"
	debounce.Parent = player

	local key = "user-" .. player.userId

	local storeditems = datastore:GetAsync(key)

	if storeditems then
		Gold.Value = storeditems[1]
		Rebirths.Value = storeditems[2]
	else
		local items = {Gold.Value, Rebirths.Value}
		datastore:SetAsync(key, items)
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local items = {player.leaderstats.Gold.Value, player.leaderstats.Rebirths.Value}
	local key = "user-" .. player.userId

	datastore:SetAsync(key, items)
end)
1 Like

Maybe try wrapping SetAsync() and GetAsync() functions in a pcall.

local success, response = pcall(function() datastore:SetAsync(key, items) end)

if not success then
    warn("Could not save the data for: " .. player.Name)
end
2 Likes
game:BindToClose(function()
	wait(3)
end)
2 Likes

so like this?:

local datastore = game:GetService("DataStoreService"):GetDataStore("Data")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"

	local Gold = Instance.new("NumberValue")
	Gold.Parent = leaderstats
	Gold.Name = "Gold"

	local Rebirths = Instance.new("NumberValue")
	Rebirths.Parent = leaderstats
	Rebirths.Name = "Rebirths"

	local debounce = Instance.new("BoolValue")
	debounce.Value = false
	debounce.Name = "Debounce"
	debounce.Parent = player

	local key = "user-" .. player.userId

	local storeditems = datastore:GetAsync(key)

	if storeditems then
		Gold.Value = storeditems[1]
		Rebirths.Value = storeditems[2]
	else
		local items = {Gold.Value, Rebirths.Value}
		datastore:SetAsync(key, items)
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local items = {player.leaderstats.Gold.Value, player.leaderstats.Rebirths.Value}
	local key = "user-" .. player.userId
	
	datastore:SetAsync(key, items)
	local success, response = pcall(function() datastore:SetAsync(key, items) end)

	if not success then
		warn("Could not save the data for: " .. player.Name)
	end
end)

Like this:

local datastore = game:GetService("DataStoreService"):GetDataStore("Data")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"

	local Gold = Instance.new("NumberValue")
	Gold.Parent = leaderstats
	Gold.Name = "Gold"

	local Rebirths = Instance.new("NumberValue")
	Rebirths.Parent = leaderstats
	Rebirths.Name = "Rebirths"

	local debounce = Instance.new("BoolValue")
	debounce.Value = false
	debounce.Name = "Debounce"
	debounce.Parent = player

	local key = "user-" .. player.userId

    local success, response = pcall(function() storedItems = datastore:GetAsync(key) end)

	if not success then
		warn("Could not get the data for: " .. player.Name)
	end

	if storeditems then
		Gold.Value = storeditems[1]
		Rebirths.Value = storeditems[2]
	else
		local items = {Gold.Value, Rebirths.Value}

		local success, response = pcall(function() datastore:SetAsync(key, items) end)

	      if not success then
		    warn("Could not save the data for: " .. player.Name)
	      end
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local items = {player.leaderstats.Gold.Value, player.leaderstats.Rebirths.Value}
	local key = "user-" .. player.userId
	
	local success, response = pcall(function() datastore:SetAsync(key, items) end)

	if not success then
		warn("Could not save the data for: " .. player.Name)
	end
end)

You need to have BindToClose at the end of the script or it won’t work.

game:BindToClose(function()
	wait(3)
end)

Example:

local datastore = game:GetService("DataStoreService"):GetDataStore("Data")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"

	local Gold = Instance.new("NumberValue")
	Gold.Parent = leaderstats
	Gold.Name = "Gold"

	local Rebirths = Instance.new("NumberValue")
	Rebirths.Parent = leaderstats
	Rebirths.Name = "Rebirths"

	local debounce = Instance.new("BoolValue")
	debounce.Value = false
	debounce.Name = "Debounce"
	debounce.Parent = player

	local key = "user-" .. player.userId

    local success, response = pcall(function() storedItems = datastore:GetAsync(key) end)

	if not success then
		warn("Could not get the data for: " .. player.Name)
	end

	if storeditems then
		Gold.Value = storeditems[1]
		Rebirths.Value = storeditems[2]
	else
		local items = {Gold.Value, Rebirths.Value}

		local success, response = pcall(function() datastore:SetAsync(key, items) end)

	      if not success then
		    warn("Could not save the data for: " .. player.Name)
	      end
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local items = {player.leaderstats.Gold.Value, player.leaderstats.Rebirths.Value}
	local key = "user-" .. player.userId
	
	local success, response = pcall(function() datastore:SetAsync(key, items) end)

	if not success then
		warn("Could not save the data for: " .. player.Name)
	end
end)

game:BindToClose(function()
	wait(3)
end)

@Denys_Developer you can use this script just change the names

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PointsStats") -- Change this with a different name.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DeathsStats") -- Change this with a different name.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("StageStats") -- Change this with a different name.

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"
	
	local Points = Instance.new("IntValue", Leaderstats)
	Points.Name = "Points" -- Change "Points" with your currency.
	Points.Value = 0 or DataStore
	
	local Deaths = Instance.new("IntValue", Leaderstats)
	Deaths.Name = "Deaths" -- Change "Deaths" with your currency.
	Deaths.Value = 0 or DataStore
	
	local Stage = Instance.new("IntValue", Leaderstats)
	Stage.Name = "Stage" -- Change "Stage" with your currency.
	Stage.Value = 1 or DataStore
	
	local Data = DataStore:GetAsync(Player.UserId)
	
	
	if Data then
		Points.Value = Data.Points -- Change "Points" with your currency
		Deaths.Value = Data.Deaths -- Change "Deaths" with your currency.
		Stage.Value = Data.Stage -- Change "Stage" with your currency.
		
	
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["Points"] = Player.leaderstats.Points.Value; -- Change "Points" with your currency.
		["Deaths"] = Player.leaderstats.Deaths.Value; -- Change "Deaths" with your currency.
		["Stage"] = Player.leaderstats.Stage.Value; -- Change "Stage" with your currency.
		
		
	})
end)