Datastore Leaderstats

I keep getting an error in the output that leader stats isn’t apart of the player. Here’s some of my code.

local DataStoreService = game:GetService("DataStoreService")
local SaveCashDataStore = DataStoreService:GetDataStore("SaveCashDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	--Cash
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Value = 0
	Cash.Parent = Leaderstats
	--Cash
	
	--Load previous data
	local Success, Error = pcall(function()
		local Data = SaveCashDataStore:GetAsync(Player.UserId)
	end)
	
	--Setting what will be saved for this instance it's the CASH!
	if Success then
		print("Sucessfully loaded data")
	else
		print("There was an error loading the data")
		warn(Error)
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local Success, Error = pcall(function()
		SaveCashDataStore:SetAsync(Player.UserId, game.Players.leaderstats.Cash.Value)
	end)
	if Success then
		print("Sucessfully saved data")
	else
		print("There was an error saving data")
		warn(Error)
	end
end)
1 Like

I’m new to making datastores so that’s why I wrote notes to help me remember.

You forgot to mention the player where you do the :SetAsync() Function.

try doing this for the cash amount there.

Player.leaderstats.Cash.Value

I did what you told me, but now it won’t even print that it successfully saved data. What should I do?

game.Players.PlayerRemoving:Connect(function(Player)
	local Success, Error = pcall(function()
		SaveCashDataStore:SetAsync(Player.UserId, Player.leaderstats.Cash.Value)
	end)
	if Success then
		print("Sucessfully saved data")
	else
		print("There was an error saving data")
		warn(Error)
	end
end)

I don’t know if this will help, but my guess is that the server shuts down before the game is done saving the data (since you’re the only player in the server while you playtest). Add this at the very end of the entire script like:

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

This makes it so that before the server shuts down it’ll take an extra 5 seconds to save everything.

use updateasync instead:

Try this:

local DataStoreService = game:GetService("DataStoreService")
local SaveCashDataStore = DataStoreService:GetDataStore("SaveCashDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	--Cash
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Value = 0
	Cash.Parent = Leaderstats
	--Cash
	
	--Load previous data
	local Success, Error = pcall(function()
		local Data = SaveCashDataStore:GetAsync(Player.UserId)
	end)
	
	--Setting what will be saved for this instance it's the CASH!
	if Success then
		print("Sucessfully loaded data")
	else
		print("There was an error loading the data")
		warn(Error)
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local Success, Error = pcall(function()
		SaveCashDataStore:SetAsync(Player.UserId, Player.leaderstats.Cash.Value)
	end)
	if Success then
		print("Sucessfully saved data")
	else
		print("There was an error saving data")
		warn(Error)
	end
end)

Changed from this:

local Success, Error = pcall(function()
	SaveCashDataStore:SetAsync(Player.UserId, game.Players.leaderstats.Cash.Value)
end)

to this:

local Success, Error = pcall(function()
	SaveCashDataStore:SetAsync(Player.UserId, Player.leaderstats.Cash.Value)
end)
game.Players.PlayerRemoving:Connect(function(Player)
	local Success, Error = pcall(function()
		SaveCashDataStore:SetAsync(Player.UserId, Player.leaderstats.Cash.Value)
	end)
	if Success then
		print("Sucessfully saved data")
	else
		print("There was an error saving data")
		warn(Error)
	end
end)

It still isn’t saving data! Hmm

Well I’m testing it out in the real game. I publish it after each script change btw.

Here’s all my code

local DataStoreService = game:GetService("DataStoreService")
local SaveCashDataStore = DataStoreService:GetDataStore("SaveCashDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	--Cash
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Value = 0
	Cash.Parent = Leaderstats
	--Cash
	
	--Load previous data
	local Success, Error = pcall(function()
		local Data = SaveCashDataStore:GetAsync(Player.UserId)
	end)
	
	--Setting what will be saved for this instance it's the CASH!
	if Success then
		print("Sucessfully loaded data")
	else
		print("There was an error loading the data")
		warn(Error)
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local Success, Error = pcall(function()
		SaveCashDataStore:SetAsync(Player.UserId, Player.leaderstats.Cash.Value)
	end)
	if Success then
		print("Sucessfully saved data")
	else
		print("There was an error saving data")
		warn(Error)
	end
end)

Does it print out an error in the output log?

Nope, it only prints Successfully loaded data. When I press stop playing it doesn’t say print the data saved and I PRESS SERVER and changed the value but it didn’t save.

Also in studio API services are on

I did, but some reason it ain’t saving anything.

Try this:

game.Players.PlayerAdded:Connect(function(Player)
	--Cash
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Value = 0
	Cash.Parent = Leaderstats
	--Cash
	
	--Save if changed
	Cash.Changed:Connect(function(Value)
		local Success, Error = pcall(function()
			SaveCashDataStore:SetAsync(Player.UserId, Value)
		end)

		if Success then
			print("Sucessfully saved data")
		else
			print("There was an error saving data")
			warn(Error)
		end
	end)

	--Load previous data
	local Success, Error = pcall(function()
		local Data = SaveCashDataStore:GetAsync(Player.UserId)
	end)
	
	--Setting what will be saved for this instance it's the CASH!
	if Success then
		print("Sucessfully loaded data")
	else
		print("There was an error loading the data")
		warn(Error)
	end
end)

What did you change? Usually I see the change and change that part in my script

Added this in Players.PlayerAdded event:

--Save if changed
	Cash.Changed:Connect(function(Value)
		local Success, Error = pcall(function()
			SaveCashDataStore:SetAsync(Player.UserId, Value)
		end)

		if Success then
			print("Sucessfully saved data")
		else
			print("There was an error saving data")
			warn(Error)
		end
	end)

Didn’t work. ADDING CHARACHTERS