Alright so today I was following one of AlvinBlox’s tutorials, and I came across the fact after I was done that the data didn’t save, and it Studio didn’t print any errors. What’s the problem?
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = leaderstats
leaderstats.Parent = player
local cash = Instance.nwe("IntValue")
cash.Name= "Cash"
cash.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-cash")
end)
if success then
cash.Value = data
else
print("There was an error while retrieving 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 an error when saving data")
warn(errormessage)
end
end)
i did not read the fact that the output didnt print out any errors, usually i would expect it to print “attempt to index nil with nwe” Hope my solution worked for you. Also make sure to change the intvalue on the server and not the client
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStoreTest1")
game.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.Parent = leaderstats
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."GameData")
end)
if success then
if data ~= nil then
cash.Value = data["Cash"]
coins.Value = data["Coins"]
end
-- Set Data if Data Exists
else
print("There was an error while retrieving data")
warn(errormessage)
end -- Warn Failure
end)
game.Players.PlayerRemoving:Connect(function(player)
if player:FindFirstChild("leaderstats") then
local data = {}
data["Cash"] = player.leaderstats.Cash.Value
data["Coins"] = player.leaderstats.Coins.Value
local tries = 0
local success
repeat
tries = tries + 1
success = pcall(function()
myDataStore:SetAsync(player.UserId.."GameData", data)
end)
if not success then wait(1) end
until tries == 5 or success
if not success then
warn("Cannot save data for player!")
end
end
end)
I rewrote everything, now saving both the cash and coins to one table. This is called a dictionary.
try this in a real game and see if your coins and cash are saved.
I made a function that retries saving your data 10 times if your data somehow doesn’t save.