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