Datastore problem!

I dont know why but this script doesnt work does anyone know why?

local dataStoreService = game:GetService("DataStoreService")
local myDataStore = dataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local clones = Instance.new("IntValue")
	clones.Name = "Clones"
	clones.Parent = leaderstats
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
	
	local data
	local succes, errormessage = pcall(function()
		myDataStore:GetAsync(player.UserId.."-clones")
	end)
	
	if succes then
		clones.Value = data
	else
		print("Error LOL")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local succes, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-clones",player.leaderstats.Clones.Value)
	end)
	
	if succes then
		print("DataSaved")
	else
		print("Error")
		warn(errormessage)
	end
end)

Replace:

with:

data = myDataStore:GetAsync(player.UserId.."-clones")

The problem is that you didn’t put the data from GetAsync into the variable.

one more question do you know how i can save multiple leaderstats

Instead of saving the data as a number value, you can save it as a table. So using your example, you could make it something like this:

local dataStoreService = game:GetService("DataStoreService")
local myDataStore = dataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local clones = Instance.new("IntValue")
	clones.Name = "Clones"
	clones.Parent = leaderstats
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
	
	local data
	local succes, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-clones")
	end)
	
	if succes then
		if data then
			clones.Value = data.clones
		end
	else
		print("Error LOL")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local savedStats = {"Clones", "Others"}
	local data = {}
	for _, stat in pairs(savedStats) do
		data[stat] = player.leaderstats[stat].Value
	end
	
	local succes, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-clones",data)
	end)
	
	if succes then
		print("DataSaved")
	else
		print("Error")
		warn(errormessage)
	end
end)

Hope this helps!

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