Datastore Leaderstats

Can you send me how your code looks after the change?

Try this as well:

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 Data
	local Success, Error = pcall(function()
		Data = SaveCashDataStore:GetAsync(tostring(Player.UserId))
	end)
	
	--Setting what will be saved for this instance it's the CASH!
	if Success then
		print("Sucessfully loaded data")
		Cash.Value = 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(tostring(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)

Wait I fixed it… wait. So I reverted back to my code then I added a variable named Data and put that in. It printed successfully saved data in the output but my data didn’t save.

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

Edited it. Copy, paste, and try again.

??? Which one??? ADDING CHARACTRERS

Edited which post?? ADDING CHARACTERS

That’s because you never set the Cash Value to Data when you loaded it in the Players.PlayerAdded event.

1 Like

Huh?? What do you mean ADDING CHARACTESR

I made it do PLayersRemoving… What do u mean

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 Data
	local Success, Error = pcall(function()
		Data = SaveCashDataStore:GetAsync(tostring(Player.UserId))
	end)
	
	--Setting what will be saved for this instance it's the CASH!
	if Success then
		print("Sucessfully loaded data")
		Cash.Value = Data
	else
		print("There was an error loading the data")
		warn(Error)
	end
end)

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

Change is in this code:

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

This should be

player.leaderstats.Cash.Value

This is in the Player Removing event.

I watched Dev King and it works now. Here’s 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
	
	--Loading in the data when a player joins
	local Data
	local Success, ErrorMessage = pcall(function()
		Data = SaveCashDataStore:GetAsync(Player.UserId)
	end)
	if Success then
		Cash.Value = Data
		print("Sucessfully loaded data")
	else
		print("There was an error loading data")
		warn(ErrorMessage)
	end
end)
--Loading in the data when a player joins

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

ok, make the post a solution since you solved it yourself

Uhhh, how would I do that? Uhhhh

So there should be a checkbox with ‘Solution’ on the message, press that

I know that but which message? ADDING CHARACTERS

Yours, the script that works that you made with the help of devking

Anyways although you already resolved this topic, I still recommend adding this in as like a extra precaution.