One datastore is working but the other isnt?

I am making a cleaning game and basically the trash datastore saves but the money datastore doesnt? Heres the script:

local DataStoreService = game:GetService("DataStoreService")

local TrashDataStore = DataStoreService:GetDataStore("TrashDataStore")

local MoneyDataStore = DataStoreService:GetDataStore("MoneyDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Value = 0
	money.Parent = leaderstats

	local totaltrash = Instance.new("IntValue")
	totaltrash.Name = "Trash"
	totaltrash.Value = 0
	totaltrash.Parent = leaderstats

	local Backpack = Instance.new("IntValue")
	Backpack.Name = "BackpackTrash"
	Backpack.Value = 0
	Backpack.Parent = leaderstats
	
	local data
	
	local sucess, errormessage = pcall(function()
		data = MoneyDataStore:GetAsync(player.UserId.."-Money")
	end)
	if sucess then
		print("Data Successfully Saved!")
	else
		print("Error Saving Data.")
		warn(errormessage)
	end
	local sucess, errormessage = pcall(function()
		data = TrashDataStore:GetAsync(player.UserId.."-Trash")
	end)
	
	if sucess then
		totaltrash.Value = data
	else
		print("There was an error getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local sucess, errormessage = pcall(function()
		MoneyDataStore:SetAsync(player.UserId.."-Money",player.leaderstats.Money.Value)
	end)
	if sucess then
		print("Data Successfully Saved!")
	else
		print("Error Saving Data.")
		warn(errormessage)
	end
	local sucess, errormessage = pcall(function()
		TrashDataStore:SetAsync(player.UserId.."-Trash",player.leaderstats.Trash.Value)
	end)
	
	if sucess then
		print("Data Successfully Saved!")
	else
		print("Error Saving Data.")
		warn(errormessage)
	end
end)

Where are you updating the Money IntValue on PlayerAdded?

How would I update it? and where would I place the update?

You type totaltrash.Value = data but not money.Value = data

2 Likes

Arthur is talking about what happens if the data is successful, you currently don’t do anything for the money.

if sucess then
 -- change the money value to your saved value
		print("Data Successfully Saved!") -- not sure why you call it saved when it's loaded
	else
		print("Error Saving Data.") -- this too
		warn(errormessage)
	end

whereas your trash values load:

if sucess then
		totaltrash.Value = data
	else
		print("There was an error getting your data")
		warn(errormessage)
	end

This is what you should have when loading the Money.

		data = MoneyDataStore:GetAsync(player.UserId.."-Money")
if (data == nil) then
-- This player is new
else
--This player has data
money.Value = data
end

This might not be right heh

You do realise that even if it is successful there could be no previous data

2 Likes

I tried this but it doesnt work

I edited the reply

this is the updated script

local DataStoreService = game:GetService("DataStoreService")

local TrashDataStore = DataStoreService:GetDataStore("TrashDataStore")

local MoneyDataStore = DataStoreService:GetDataStore("MoneyDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Value = 0
	money.Parent = leaderstats

	local totaltrash = Instance.new("IntValue")
	totaltrash.Name = "Trash"
	totaltrash.Value = 0
	totaltrash.Parent = leaderstats

	local Backpack = Instance.new("IntValue")
	Backpack.Name = "BackpackTrash"
	Backpack.Value = 0
	Backpack.Parent = leaderstats

	local data

	local sucess, errormessage = pcall(function()
		data = MoneyDataStore:GetAsync(player.UserId.."-Money")
	end)
	if sucess then
		money.Value = data
	else
		print("There was an error getting your data")
		warn(errormessage)
	end
	local sucess, errormessage = pcall(function()
		data = TrashDataStore:GetAsync(player.UserId.."-Trash")
	end)

	if sucess then
		totaltrash.Value = data
	else
		print("There was an error getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local sucess, errormessage = pcall(function()
		MoneyDataStore:SetAsync(player.UserId.."-Money",player.leaderstats.Money.Value)
	end)
	if sucess then
		print("Data Successfully Saved!")
	else
		print("Error Saving Data.")
		warn(errormessage)
	end
	local sucess, errormessage = pcall(function()
		TrashDataStore:SetAsync(player.UserId.."-Trash",player.leaderstats.Trash.Value)
	end)

	if sucess then
		print("Data Successfully Saved!")
	else
		print("Error Saving Data.")
		warn(errormessage)
	end
end)

I would get rid of the sucess/error when loading and give the money & trash individual variables (ie MoneyData TrashData)

local MoneyData = MoneyDataStore:GetAsync(player.UserId.."-Money")
local TrashData = TrashDataStore:GetAsync(player.UserId.."-Trash")

if (MoneyData ~= nil) then
--Player has data
else
--Player has no data, this player is new
end

if (TrashData ~= nil) then
--Player has data
else
--Player has no data, this player is new
end

Try this in your playeradded event:

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local money = Instance.new("IntValue", leaderstats)
	money.Name = "Money"
	money.Value = 0

	local totaltrash = Instance.new("IntValue", leaderstats)
	totaltrash.Name = "Trash"
	totaltrash.Value = 0

	local Backpack = Instance.new("IntValue", leaderstats)
	Backpack.Name = "BackpackTrash"
	Backpack.Value = 0

	local Success1, Money = pcall(function()
		return MoneyDataStore:GetAsync(player.UserId.."-Money")
	end)
	
	local Success2, Trash = pcall(function()
		return TrashDataStore:GetAsync(player.UserId.."-Trash")
	end)
	
	if Success1 then
		-- Successfully attempted to retrieve player data
		if Money then -- Player is not a new player, because there is previous data
			money.Value = Money
		end
	end
	
	if not Success1 then
		-- Failed to retrieve data
	end
	
	if Success2 then
		-- Successfully attempted to retrieve player data
		if Trash then -- Player is not a new player, because there is previous data
			totaltrash.Value = Trash
		end
	end
	
	if not Success2 then
		-- Failed to retrieve data
	end
	
end)

Do I need to edit anything? or just use it like this?

you can use it like that I’ve tested it and it worked perfectly.

it still does not save my money, as you can see here it did save trash from previous tests but not money:

image

That’s weird I’ve copied your whole code and tested it and it saved both Money and Trash

heres the updated code if i did anything wrong

------------------
-- DATA STORES
-----------------

local DataStoreService = game:GetService("DataStoreService")
local TrashDataStore = DataStoreService:GetDataStore("TrashDataStore")
local MoneyDataStore = DataStoreService:GetDataStore("MoneyDataStore")


------------------
-- LEADERSTATS
-----------------
game.Players.PlayerAdded:Connect(function(player)

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

	local money = Instance.new("IntValue", leaderstats)
	money.Name = "Money"
	money.Value = 0

	local totaltrash = Instance.new("IntValue", leaderstats)
	totaltrash.Name = "Trash"
	totaltrash.Value = 0

	local Backpack = Instance.new("IntValue", leaderstats)
	Backpack.Name = "BackpackTrash"
	Backpack.Value = 0

	local Success1, Money = pcall(function()
		return MoneyDataStore:GetAsync(player.UserId.."-Money")
	end)

	local Success2, Trash = pcall(function()
		return TrashDataStore:GetAsync(player.UserId.."-Trash")
	end)

	if Success1 then
		-- Successfully attempted to retrieve player data
		if Money then -- Player is not a new player, because there is previous data
			money.Value = Money
		end
	end

	if not Success1 then
		-- Failed to retrieve data
	end

	if Success2 then
		-- Successfully attempted to retrieve player data
		if Trash then -- Player is not a new player, because there is previous data
			totaltrash.Value = Trash
		end
	end

	if not Success2 then
		-- Failed to retrieve data
	end

end)
------------------
-- PLAYER REMOVING SAVE
-----------------
game.Players.PlayerRemoving:Connect(function(player)
	
------------------
-- MONEY DATASTORE
-----------------
	
	local sucess, errormessage = pcall(function()
		MoneyDataStore:SetAsync(player.UserId.."-Money",player.leaderstats.Money.Value)
	end)
	if sucess then
		print("Data Successfully Saved!")
	else
		print("Error Saving Data.")
		warn(errormessage)
	end
	
------------------
-- TRASH DATASTORE
	----------------	

	local sucess, errormessage = pcall(function()
		TrashDataStore:SetAsync(player.UserId.."-Trash",player.leaderstats.Trash.Value)
	end)

	if sucess then
		print("Data Successfully Saved!")
	else
		print("Error Saving Data.")
		warn(errormessage)
	end
end)

Did you update the leaderstats via the client or the server, because you’ll need to change it on the server or it won’t save.

im already done most the simulator so i figured out the selling proccess and collecting so I test that way and it updates the leaderstats when im in the game but not when i rejoin

Did you publish the game yet or it wont work? or did you do it in studio