I am having problems with my Datastores. And what really confuses me is that there are no errors coming up so I can’t figure it out that way. Would anybody have any solutions?
local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(player)
local LeaderStats = Instance.new("Folder", player)
LeaderStats.Name = "leaderstats"
local Cash = Instance.new("IntValue", LeaderStats)
Cash.Name = "Cash"
local Data
local Success, ErrorMessage = pcall (function()
Data = MyDataStore:GetAsync(player.UserId.."-Cash")
end)
if Success then
Cash.Value = Data
print(Data)
print("Player data was successfully retrieved")
else
print("There was an error retrieving your data")
warn(ErrorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local Success, ErrorMessage = pcall (function()
MyDataStore:SetAsync(player.UserId.."-Cash", player.leaderstats.Cash.Value)
end)
if Success then
print("Player data successfully saved")
else
print("There was a problem saving the data")
warn(ErrorMessage)
end
end)
According to the output it is saying that scripts are being saved and retrieved but the value is simply 0 which it shouldn’t be.
local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(player)
local LeaderStats = Instance.new("Folder", player)
LeaderStats.Name = "leaderstats"
local Cash = Instance.new("IntValue", LeaderStats)
Cash.Name = "Cash"
local Data
local Success, ErrorMessage = pcall (function()
Data = MyDataStore:GetAsync(player.UserId.."Cash")
end)
if Success then
Cash.Value = Data
print(Data)
print("Player data was successfully retrieved")
else
print("There was an error retrieving your data")
warn(ErrorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local Success, ErrorMessage = pcall (function()
MyDataStore:SetAsync(player.UserId.."Cash", player.leaderstats.Cash.Value)
end)
if Success then
print("Player data successfully saved")
else
print("There was a problem saving the data")
warn(ErrorMessage)
end
end)
You said “-Cash”
local Success, ErrorMessage = pcall (function()
Data = MyDataStore:GetAsync(player.UserId.."-Cash")
end)
I have been able to get it working by simply re-writing the entire thing from scratch. Here’s the finish product:
I made two separate scripts one for Leaderstats and one for Datastores
Datastores:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local cashStore = DataStoreService:GetDataStore("PlayerMoney")
Players.PlayerAdded:Connect(function(player)
local success, currentCash = pcall(function()
return cashStore:GetAsync(player.UserId)
end)
if success then
player.leaderstats.Cash.Value = currentCash
end
end)
Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
cashStore:SetAsync(player.UserId, player.leaderstats.Cash.Value)
end)
if success then
print("Success!")
else
warn(err)
end
end)
Leaderstats:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
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
end)