Hello, I am making a test place for developing and I am currently testing out datastores.
However, the data cannot save.
The code:
local dss = game:GetService("DataStoreService")
local data = dss:GetDataStore("DataStore")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder", plr)
leaderstats.Name = "leaderstats"
local cash = Instance.new("IntValue", leaderstats)
cash.Name = "Cash"
local playerId = "Player_" .. plr.UserId
local plrdata
local success, error = pcall(function()
plrdata = data:GetAsync("playerId")
end)
if success then
cash.Value = plrdata
print("Data loaded for player!")
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local playerId = "Player_" .. plr.UserId
local attempt = 0
repeat
local data = plr.leaderstats.Cash.Value
local success, error = pcall(function()
data:SetAsync(playerId, data)
end)
if success then
print("Data saved for player!")
else
warn("Data could not be saved for player!")
end
attempt += 1
until success or attempt == 5
end)
The console always gives the warning in the saving code. Any advice? I’m testing in Roblox Studio, if that matters at all.
if success then
print("Data saved for player!")
else
warn("Data could not be saved for player!")
print(error)
end
I’m hedging my bets however that you probably need to enable access to the datastore in studio through settings. If not, the error will tell us the issue!
You really shouldn’t use “error” as the keyword is already occupied, but this will not be an issue in this context as you’ll overwrite it.
Found your problem, you need to use another variable then data as shown here:
game.Players.PlayerRemoving:Connect(function(plr)
local playerId = "Player_" .. plr.UserId
local attempt = 0
repeat
local mydata = plr.leaderstats.Cash.Value
local success, errortxt = pcall(function()
data:SetAsync(playerId, mydata)
end)
if success then
print("Data saved for player!")
else
warn("Data could not be saved for player!")
print(errortxt)
end
attempt += 1
until success or attempt == 5
end)