I have recently began learning datastores and have ran into an issue where it is not saving.
I am not sure if it is just an issue with my code, or if the datastore does not work in Studio, but here is my current code.
Summary
local DSS = game:GetService("DataStoreService")
local CurrentDS = DSS:GetDataStore("1") -- This can be changed to load a new data store
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
coins.Value = 0
local stage = Instance.new("IntValue")
stage.Name = "Stage"
stage.Parent = leaderstats
stage.Value = 1
--Sets Data to the Player's ID
local PlayerID = "Player_"..player.UserId
-- Loads Data
local save
local suc, err = pcall(function()
save = CurrentDS:GetAsync(PlayerID)
end)
if suc then -- Actually loads the data
if save then
coins.Value = save.Coins
stage.Value = save.Stage
end
end
end)
-- SAVE IS NOT WORKING
-- Saving Player's Data
Players.PlayerRemoving:Connect(function(player)
local PlayerID = "Player_"..player.UserId
local save = {
Coins = player.leaderstats.Coins.Value;
Stage = player.leaderstats.Stage.Value
}
local suc, err = pcall(function()
CurrentDS:SetAsync(PlayerID, save)
end)
if suc then
print("Data Saved")
else
print("Error during save")
warn(err)
end
end)
It will sometimes save, but it seems to randomly work, I’m not really sure what is going on with it. For instance, I did 10 stages of game and stopped the test and it did not save; Then I did the 10 stages again, and it saved.
The last few times that I tried to save something, it did not work.
use save[1]
save[2] and delete the table of save and use this instead of currentds:setasync(playerid, save)
replace it with CurrentDS:SetAsync(PlayerID, player.leaderstats.Coins.Value, player.leaderstats.Stage.Value)
This wouldn’t work. :SetAsync takes 2 required arguments, a key and a value. You are allowed to store tables inside of a DataStore, and if you’re dealing with large amounts of data, you should probably save data using tables rather than using separate keys.
How would I check for it? I always assumed playeradded worked whenever someone was added to the game?
Regardless of that, the save is what the current issue is, the pcall prints do not happen.
local suc, err = pcall(function()
CurrentDS:SetAsync(PlayerID, save)
end)
if suc then
print("Data Saved")
else
print("Error during save")
warn(err)
end
I have seen them in the output a couple of times after stopping a test so I know that they should, but that hardly happens.
Because you’re still assuming that player had code ran for them in the playeradded event, in which case the first player has a 1/8 chance of having the event fire for them/
They do, please do not reply with baseless information if you aren’t knowledgeable thanks, it helps prevent misinformation from people who don’t know what they are talking about. OP’s problem is that he isn’t checking for current players that join before PlayerAdded is fired. Datastores work the same way in studio as they do in a live server, the only condition is that API services are enabled in studio’s permissions.
local DSS = game:GetService("DataStoreService")
local CurrentDS = DSS:GetDataStore("1") -- This can be changed to load a new data store
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
coins.Value = 0
local stage = Instance.new("IntValue")
stage.Name = "Stage"
stage.Parent = leaderstats
stage.Value = 1
--Sets Data to the Player's ID
local PlayerID = "Player_"..player.UserId
-- Loads Data
local save
local suc, err = pcall(function()
save = CurrentDS:GetAsync(PlayerID)
end)
if suc then -- Actually loads the data
if save then
coins.Value = save.Coins
stage.Value = save.Stage
end
end
end)
local function createdatatable(player)
local player_data = {}
for _, data in pairs(player.leaderstats:GetChildren()) do
player_data[data.Name] = data.Value
end
return player_data
end
-- Saving Player's Data
Players.PlayerRemoving:Connect(function(player)
local PlayerID = "Player_"..player.UserId
local suc, err = pcall(function()
CurrentDS:SetAsync(PlayerID,createdatatable(player))
end)
if suc then
print("Data Saved")
else
print("Error during save")
warn(err)
end
end)
So how exactly would I get around this? I have always assumed that PlayerAdded always worked, as the script is in ServerScriptStorage, and would load in as the server started and before the player conencted, so it would be working by the time they were loaded in?