Hey there! I’m trying to use DataStores to save a value located in a folder that I put in the player and a folder that is located in their backpack. I got the value down, however the folder doesn’t save. I’m pretty new to saving data, so not sure what I’m doing wrong here, but take a look at my script.
local DataStore = game:GetService("DataStoreService")
local GameData = DataStore:GetDataStore("Data 1")
game.Players.PlayerAdded:Connect(function(Player)
local PlrStats = Instance.new("Folder",Player)
PlrStats.Name = "PlrStats"
local Stand = Instance.new("StringValue",PlrStats)
Stand.Name = "Stand"
Stand.Value = ""
local saves
pcall(function()
saves = GameData:GetAsync(Player.UserId)
end)
if saves then
warn("Player data loaded.")
Stand.Value = saves
else
warn("Player data was not found.")
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local PlrStats = Player:WaitForChild("PlrStats")
saveData(Player,PlrStats)
end)
function saveData(Player,PlrStats)
local Stand = PlrStats:WaitForChild("Stand")
GameData:SetAsync(Player.UserId, Stand.Value)
warn("Saved a player's data.")
end
local DataStore = game:GetService("DataStoreService")
local GameData = DataStore:GetDataStore("Data 1")
game.Players.PlayerAdded:Connect(function(Player)
local PlrStats = Instance.new("Folder",Player)
PlrStats.Name = "PlrStats"
local Stand = Instance.new("StringValue",PlrStats)
Stand.Name = "Stand"
Stand.Value = ""
local saves
pcall(function()
saves = GameData:GetAsync(Player.UserId)
end)
if saves then
warn("Player data loaded.")
Stand.Value = saves
else
warn("Player data was not found.")
end
end)
function saveData(Player,PlrStats)
local Stand = PlrStats:WaitForChild("Stand")
local success, errormessage = pcall(function()
GameData:SetAsync(Player.UserId, Stand.Value)
warn("Saved a player's data.")
end)
if success then
print("worked and saved")
else
print("failed to save: ", errormessage)
end
end
game.Players.PlayerRemoving:Connect(function(Player)
local PlrStats = Player:WaitForChild("PlrStats")
saveData(Player,PlrStats)
end)
You could use a default “stand” value, at 0 at the start of your code. You could also use a key when pcalling for your datastore. If the player has no data, you give them the default stand value else give them the data that they already have.
I would do something along these lines, also you mentioned you were trying to save the folder? Do you mean the folder object? If so, I believe you aren’t able to save object values in DataStores.
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local GameData = DataStoreService:GetDataStore("GameData")
function saveData(Player, Data)
local Success, Error = pcall(function()
GameData:SetAsync(Player.UserId, Data)
end)
if Success then
print("Saved a player's data.")
else
warn(Error)
end
end
Players.PlayerAdded:Connect(function(Player)
local PlrStats = Instance.new("Folder", Player)
PlrStats.Name = "PlrStats"
local Stand = Instance.new("StringValue", PlrStats)
Stand.Name = "Stand"
Stand.Value = "Default" -- Set this to the default value you'd like to have
local Success, Stand = pcall(function()
local Stand = GameData:GetAsync(Player.UserId)
return Stand
end)
if Success then
print("Player data loaded.")
Stand.Value = Stand
else
warn(Stand)
end
end)
Players.PlayerRemoving:Connect(function(Player)
local PlrStats = Player:FindFirstChild("PlrStats")
local Data = nil
if PlrStats and PlrStats:FindFirstChild("Stand") then
Data = PlrStats.Stand.Value
end
if Data then
saveData(Player, Data)
end
end)
Yeah, I’m trying to save a folder located inside the player called “PlrStats”. Inside PlrStats is a value. I’m also trying to save a folder located in the player’s backpack, but the only thing that is saving is the PlrStats folder and the value inside of it.
I’m not saving tools, though. The reason a folder is in there is because I’m making an anime based game, and they are able to call out this “stand”. The stand is inside that folder, but it won’t save.