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)
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
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 MoneyDataTrashData)
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
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)
------------------
-- 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)
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