Datastore broken

  1. What do you want to achieve? A datastore. Saves 2 values on leaderstats

  2. What is the issue? Will not save.

  3. What solutions have you tried so far? I looked at many tutorials on youtube and some posts on the devforum, none were able to help me.

local serverStorage = game:GetService("ServerStorage")
local Datastore = game:GetService("DataStoreService"):GetDataStore("PlayerSave3")
game.Players.PlayerAdded:Connect(function(player)
	
	
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local time = Instance.new("NumberValue")
	time.Name = "Time"
	time.Parent = leaderstats
	
	local rebirths = Instance.new("IntValue")
	rebirths.Name = "Rebirths"
	rebirths.Parent = leaderstats
	
	
	local dataFolder = Instance.new("Folder")
	dataFolder.Name = player.Name
	dataFolder.Parent = serverStorage.RemoteData
	
	local timeData, rebirthsData
	
	local success,errormessage = pcall(function()
		timeData = Datastore:GetAsync("time-"..player.UserId)
		rebirthsData = Datastore:GetAsync("rebirths-"..player.UserId)
		
	end)
	if success then
		if timeData then
			time.Value = timeData
			rebirths.Value = rebirthsData
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		Datastore:SetAsync("time-"..player.UserId,player.leaderstats.Time.Value)
		Datastore:SetAsync("rebirths-"..player.UserId,player.leaderstats.Rebirths.Value)
	end)
end)

NOTE IM QUITE NEW TO SCRIPTING

are you unable to tell me what is wrong with what i currently have?

That’s probably because you didn’t connected it into the game:BindToClose() function which will run everytime the Server is about to close. It’s always good to use this since it will save the Data of all Players whenever the Server closes. Add this code below at the end of your Script - This should take care of it.

Another thing that you can do is try using ProfileService - A powerful module for Saving, Managing and Updating Data, It is currently the best Module for Data Saving so far and i really recommend you using it.

game:BindToClose(function()
	-- Always run this on a pcall in-case some Internal error
	-- or whatever else happens
	pcall(function()
		for Index, Player in pairs(game:GetService("Players"):GetPlayers()) do
			if Index and Player then
				local success, errormessage = pcall(function()
					Datastore:SetAsync("time-"..player.UserId,player.leaderstats.Time.Value)
					Datastore:SetAsync("rebirths-"..player.UserId,player.leaderstats.Rebirths.Value)
				end)
			end
		end
	end)
end)

thanks so much for the help
just curious, where would i put this?

At the end of your Data Saving script right under the PlayerRemoving Event.

so all in all it should look like

local Datastore = game:GetService("DataStoreService"):GetDataStore("PlayerSave3")
game.Players.PlayerAdded:Connect(function(player)



	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

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

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


	local dataFolder = Instance.new("Folder")
	dataFolder.Name = player.Name
	dataFolder.Parent = serverStorage.RemoteData

	local timeData, rebirthsData

	local success,errormessage = pcall(function()
		timeData = Datastore:GetAsync("time-"..player.UserId)
		rebirthsData = Datastore:GetAsync("rebirths-"..player.UserId)

	end)
	if success then
		if timeData then
			time.Value = timeData
			rebirths.Value = rebirthsData
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		Datastore:SetAsync("time-"..player.UserId,player.leaderstats.Time.Value)
		Datastore:SetAsync("rebirths-"..player.UserId,player.leaderstats.Rebirths.Value)
	end)
end)
game:BindToClose(function()
	-- Always run this on a pcall in-case some Internal error
	-- or whatever else happens
	pcall(function()
		for Index, Player in pairs(game:GetService("Players"):GetPlayers()) do
			if Index and Player then
				local success, errormessage = pcall(function()
					Datastore:SetAsync("time-"..player.UserId,player.leaderstats.Time.Value)
					Datastore:SetAsync("rebirths-"..player.UserId,player.leaderstats.Rebirths.Value)
				end)
			end
		end
	end)
end)

because if so, that didn’t work

If your still there, try this:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local SaveDataStore = DataStoreService:GetDataStore("PlayerSave3")

local function SavePlayerData(player)
	
	local success,errormsg = pcall(function()
	
		local SaveData = {}
		
		for i,stats in pairs(player.leaderstats:GetChildren()) do
			
			SaveData[stats.Name] = stats.Value
		end	
		SaveDataStore:SetAsync(player.UserId,SaveData)
	end)
	
	if not success then
		return errormsg
	end			
end	

Players.PlayerAdded:Connect(function(player)
	
	local Stats = Instance.new("Folder")
	Stats.Name = "leaderstats"
	Stats.Parent = player
		
	local time = Instance.new("IntValue")
	time.Name = "Time"
	time.Parent = leaderstats
	
	local rebirths = Instance.new("IntValue")
	rebirths.Name = "Rebirths"
	rebirths.Parent = leaderstats
	
	local Data = SaveDataStore:GetAsync(player.UserId)
	
	if Data then
			
		print("âś…")
		
		for i,stats in pairs(Stats:GetChildren()) do
			
			stats.Value = Data[stats.Name]			
		end			
	else		
		print("❌")			
	end
end)

Players.PlayerRemoving:Connect(function(player)
	
	local errormsg = SavePlayerData(player)
	print("Saved")
	
	if errormsg then	
		warn(errormsg)		
	end	
end)

game:BindToClose(function()
	for i,player in pairs(Players:GetPlayers()) do	
		
		local errormsg = SavePlayerData(player)
		if errormsg then
			warn(errormsg)
		end			
	end
	wait(2)	
end)
1 Like

many unknown globals for leaderstats

Show me the errors -----------------------

image

I got a script that I could share it with you if you want to look at it and change the values

local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("DataStore")


game.Players.PlayerAdded:Connect(function(player)
	
	--[[
	Leaderstats
	 - Parent Folder for stats
	]]
		local leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = player
	
	
	
	--[[
	Coins
	]]
		local Coins = Instance.new("IntValue")
		Coins.Name = "Coins" 			-- used for general buildings, in shops etc.
		Coins.Value = 0
		Coins.Parent = leaderstats
	
	
	
	--[[
	Aether
	]]
		local Aether = Instance.new("IntValue")
		Aether.Name = "Aether"			-- used for battles and specific buildings
		Aether.Value = 0
		Aether.Parent = leaderstats
	
	
	
	local Gems = Instance.new("IntValue")
	Gems.Name = "Gems"				-- used to speed-up work and to buy special items
	Gems.Value = 0
	Gems.Parent = leaderstats
	
	
	
	local data
	
	local success, errorMessage = pcall(function()
		data = DataStore:GetAsync(player.UserId)
	end)
	
	if success and data ~= nil then -- CHANGE HERE COINS/AETHER/GEMS
		print("Data successfully loaded!")
		Coins.Value = data.Coins
		Aether.Value = data.Aether
		Gems.Value = data.Gems
	else
		warn(errorMessage)
	end
end)


game.Players.PlayerRemoving:Connect(function(player) -- CHANGE HERE COINS/AETHER/GEMS
	local leaderstats = player.leaderstats
	local data = {
		Coins  = leaderstats.Coins.Value;
		Aether = leaderstats.Aether.Value;
		Gems   = leaderstats.Gems.Value;
	}
	local success, errorMessage = pcall(function()
		DataStore:SetAsync(player.UserId,data)
	end)
	if success then
		print("Data successfully saved!")
	else
		warn(errorMessage)
	end
end)

Please ignore the random comments in my script, I used those for references. In this case you got Coins, Aether and Gems. Change those to whatever you want (both at the start of the code where they’re first introduced and later, when used for DataStore

Edit: Also, to remove any possible bugs related to how they’re written, Always use uppercase on first letter of your currency (coins → Coins)

I have tried doing a very new version of your Code and i hopefully fixed alot of errors and made it more readable. It should work now, Let me know if it works.

-- Services
local DataStoreService = game:GetService("DataStoreService")
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")

-- DataStore
local PlayerSaveDataStore = DataStoreService:GetDataStore("PlayerSave3")

-- Local Functions
local function LoadData(Player)
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player

	local Time = Instance.new("IntValue")
	Time.Name = "Time"
	Time.Parent = Leaderstats

	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirths"
	Rebirths.Parent = Leaderstats

	local DataFolder = Instance.new("Folder")
	DataFolder.Name = Player.Name
	DataFolder.Parent = ServerStorage:FindFirstChild("RemoteData") or warn("No RemoteData Folder was found inside ServerStorage")

	local TimeData;
	local RebirthData;

	local Ok, Result = pcall(function()
		TimeData = PlayerSaveDataStore:GetAsync("time-"..Player.UserId)
		RebirthData = PlayerSaveDataStore:GetAsync("rebirths-"..Player.UserId)
	end)

	if Ok then
		if TimeData ~= nil then
			Time.Value = TimeData
		end

		if RebirthData ~= nil then
			Rebirths.Value = RebirthData
		end
	else
		-- An error happened;
		-- If Player is in game we're gonna kick them and send a message with the error reason.
		if Player then
			pcall(function()
				Player:Kick("\nAn unexpected error happened while trying to Load your Data.\nPlease, Wait a few moments and re-join the Game!")
			end)
		end

		task.spawn(function()
			return error(string.format("[DataStore]: An unexpected error happened while trying to Load %s's Data:\n%s", Player.Name, tostring(Result)), 3)
		end)
	end
end

local function SaveData(Player)
	local Ok, Result = pcall(function()
		PlayerSaveDataStore:SetAsync("time-"..Player.UserId, Player.leaderstats.Time.Value)
		PlayerSaveDataStore:SetAsync("rebirths-"..Player.UserId, Player.leaderstats.Rebirths.Value)
	end)

	-- Save Data didn't work? Send an error message.
	if not Ok then
		task.spawn(function()
			return error(string.format("[DataStore]: An unexpected error happened while trying to Save %s's Data:\n%s", Player.Name, tostring(Result)), 3)
		end)
	end
end

-- Events & Connections
local PlayerAddedConnection = Players.PlayerAdded:Connect(LoadData)
local PlayerRemovingConnection = Players.PlayerRemoving:Connect(SaveData)

-- BindToClose
game:BindToClose(function()
	-- Save data to all Players
	pcall(function()
		for Index, Player in pairs(Players:GetPlayers()) do
			if Index and Player then
				pcall(SaveData, Player)
			end
		end
	end)
end)

-- Run the LoadData function to all Players in-case they Joined before the Script ran
for Index, Player in pairs(Players:GetPlayers()) do
	if Index and Player then
		task.spawn(function()
			pcall(LoadData, Player)
		end)
	end
end

Try this, hope it works.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local SaveDataStore = DataStoreService:GetDataStore("PlayerSave3")

local function SavePlayerData(player)
	
	local success,errormsg = pcall(function()
	
		local SaveData = {}
		
		for i,stats in pairs(player.leaderstats:GetChildren()) do
			
			SaveData[stats.Name] = stats.Value
		end	
		SaveDataStore:SetAsync(player.UserId,SaveData)
	end)
	
	if not success then
		return errormsg
	end			
end	

Players.PlayerAdded:Connect(function(player)
	
	local Stats = Instance.new("Folder")
	Stats.Name = "leaderstats"
	Stats.Parent = player
		
	local statstime = Instance.new("IntValue")
	statstime .Name = "Time"
	statstime .Parent = leaderstats
	
	local rebirths = Instance.new("IntValue")
	rebirths.Name = "Rebirths"
	rebirths.Parent = leaderstats
	
	local Data = SaveDataStore:GetAsync(player.UserId)
	
	if Data then
			
		print("âś…")
		
		for i,stats in pairs(Stats:GetChildren()) do
			
			stats.Value = Data[stats.Name]			
		end			
	else		
		print("❌")			
	end
end)

Players.PlayerRemoving:Connect(function(player)
	
	local errormsg = SavePlayerData(player)
	print("Saved")
	
	if errormsg then	
		warn(errormsg)		
	end	
end)

game:BindToClose(function()
	for i,player in pairs(Players:GetPlayers()) do	
		
		local errormsg = SavePlayerData(player)
		if errormsg then
			warn(errormsg)
		end			
	end
	wait(2)	
end)
``

also you probably didn’t specify “leaderstats”…

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

Nevermind, sorry. I see that you did write this

image

Doesn’t save. Can’t see any errors.

Try mine --------------------------

also try mine. In short, most of these scripts are sort of the same

didnt work. doesn’t save ---------------