Datastore not saving

Alright. Now everything is good. Can you see the README folder in workspace?

one second ill check now… (30) characters

The values are not in the order I needed them ;-;

Yes I see the READ script in workspace

Also maybe I have an idea of why it is not working. You should set the UserId as the second param. I rewrote your code for you. See if this works. `local dataStoreService = game:GetService(“DataStoreService”)
local players = game:GetService(“Players”)
local mainStore = dataStoreService:GetDataStore(“MainDataStore”)

players.PlayerAdded:Connect(function(player)

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

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

local success,data = pcall(function()
	return mainStore:GetAsync(player.UserId)
end)
if success then
	Subscribers.Value = data
else
	print("Failed to load data")
end

end)

players.PlayerRemoving:Connect(function(player)

local leaderstats = player:WaitForChild("leaderstats")
local Subscribers = leaderstats:WaitForChild("Subscribers")

local success,err = pcall(function()
	return mainStore:SetAsync(Subscribers.Value,player.UserId)
end)
if not success then
	print(err)
end

end)`

Let me know if it works, and if it doesn’t work just let me know as well.

Want to keep trying profileservice? Are you letting go? Because I see that it took so long for someone to reply XD

Thank you ill test it now. one sec and could you help me and add 2 values? I need Subscribers and Money. Ima test both datastores

Yeah I can help you, give me a moment though.

I also don’t know how to make the script bind to close I’m about to test your script though

local Data = game:GetService("DataStoreService"):GetDataStore("balhdwdqwi") -- enter anything here
game.Players.PlayerAdded:Connect(function(plr)
	local s = Instance.new("Folder",plr)
	s.Name = "leaderstats"
	
	local tr = Instance.new("NumberValue",s)
	tr.Name = "Subscribers"
	tr.Value = 1
	
	local sq = Instance.new("NumberValue",s)
	sq.Name = "Money"
	sq.Value = 1
	
	
	local dataload = Data:GetAsync(tostring(plr.UserId))
	
	if dataload then
		tr.Value = dataload[1] or 1
		sq.Value = dataload[2] or 1
	end
	
	
	
	game.Players.PlayerRemoving:Connect(function(plr)
		
		Data:SetAsync(tostring(plr.UserId),{
			
			plr.leaderstats.Subscribers.Value,
			plr.Stats.Money.Value
		}) 
	end)
end)



Again, this is probably not the best method of saving data, and there are much better alternatives.

But feel free to use this, it’s all yours!

It didn’t save. I think it’s something with my studio because I’ve been trying datastore scripts for a while do you know what it could be??? I have api service on

You need to test your datastores in game.

I got it

local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local Players = game.Players

local database = DataStoreService:GetDataStore("data")
local sessionData = {}

local function PlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Money = Instance.new("NumberValue")
	Money.Name = "Money"
	Money.Parent = leaderstats

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

	local success = nil
	local PlayerData = nil
	local attempt = 1

	repeat 
		success,PlayerData = pcall(function()
			return database:GetAsync(player.UserId) 
		end)

		attempt += 1

		if not success then
			warn(PlayerData)
			task.wait(3)
		end
	until success or attempt == 5

	if success then
		print("Connected to database")
		if not PlayerData then
			print("Assigning default data")
			PlayerData = {
				["Money"] = 0;
				["Subscribers"] = 0;
			}	
		end
		sessionData[player.UserId] = PlayerData
	else
		warn("Unable to get data for "..player.UserId)
		player:Kick("Unable to load your data. Try again later")

	end

	--Character 1
	
	Money.Value = sessionData[player.UserId].Money
	Money.Changed:Connect(function()
		sessionData[player.UserId].Money = Money.Value
	end)
	
	Subscribers.Value = sessionData[player.UserId].Subscribers
	Subscribers.Changed:Connect(function()
		sessionData[player.UserId].Subscribers = Subscribers.Value
	end)
end

local function Save(player)
	if sessionData[player.UserId] then
		local success = nil
		local errorMsg = nil
		local attempt = 1

		repeat 
			success,errorMsg = pcall(function()
				database:SetAsync(player.UserId, sessionData[player.UserId]) 
			end)

			attempt += 1

			if not success then
				warn(errorMsg)
				task.wait(3)
			end
		until success or attempt == 5

		if success then
			print("Data saved for "..player.Name)
		else
			warn("Unable to save for ".. player.Name)
		end
	end
end

local function ServerShutdown()
	if RunService:IsStudio() then
		return
	end

	print("Server hutting down...")
	for i, player in ipairs(Players:GetPlayers()) do
		task.spawn(function()
			Save(player)
		end)

	end
end


Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(Save)
game:BindToClose(ServerShutdown)















--game:BindToClose(function(player)
--for i, player in pairs(game.Players:GetChildren()) do
--Save(player)
--end
--end)

Okay one sec let me test this new code

Remember, don’t change it without script.

Do I need to wait before leaving the game??

No you don’t need to wait, it’s instant

It doesnt even work in game ! :sob:

Bro, catch it

local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local Players = game.Players

local database = DataStoreService:GetDataStore("data")
local sessionData = {}

local function PlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Money = Instance.new("NumberValue")
	Money.Name = "Money"
	Money.Parent = leaderstats

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

	local success = nil
	local PlayerData = nil
	local attempt = 1

	repeat 
		success,PlayerData = pcall(function()
			return database:GetAsync(player.UserId) 
		end)

		attempt += 1

		if not success then
			warn(PlayerData)
			task.wait(3)
		end
	until success or attempt == 5

	if success then
		print("Connected to database")
		if not PlayerData then
			print("Assigning default data")
			PlayerData = {
				["Money"] = 0;
				["Subscribers"] = 0;
			}	
		end
		sessionData[player.UserId] = PlayerData
	else
		warn("Unable to get data for "..player.UserId)
		player:Kick("Unable to load your data. Try again later")

	end

	--Character 1
	
	Money.Value = sessionData[player.UserId].Money
	Money.Changed:Connect(function()
		sessionData[player.UserId].Money = Money.Value
	end)
	
	Subscribers.Value = sessionData[player.UserId].Subscribers
	Subscribers.Changed:Connect(function()
		sessionData[player.UserId].Subscribers = Subscribers.Value
	end)
end

local function Save(player)
	if sessionData[player.UserId] then
		local success = nil
		local errorMsg = nil
		local attempt = 1

		repeat 
			success,errorMsg = pcall(function()
				database:SetAsync(player.UserId, sessionData[player.UserId]) 
			end)

			attempt += 1

			if not success then
				warn(errorMsg)
				task.wait(3)
			end
		until success or attempt == 5

		if success then
			print("Data saved for "..player.Name)
		else
			warn("Unable to save for ".. player.Name)
		end
	end
end

local function ServerShutdown()
	if RunService:IsStudio() then
		return
	end

	print("Server hutting down...")
	for i, player in ipairs(Players:GetPlayers()) do
		task.spawn(function()
			Save(player)
		end)

	end
end


Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(Save)
game:BindToClose(ServerShutdown)















--game:BindToClose(function(player)
--for i, player in pairs(game.Players:GetChildren()) do
--Save(player)
--end
--end)