DataStore Trouble

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
1 Like

why is the function below it

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)

use pcalls to save data ok?

2 Likes

ok, ill try right now [30 chars]

did not work, just has the value like usual

datastores is acutally hard can you try a datastore wrapper like DataStore2 and see if it works

so sorry, don’t know how to do that either… i’ve never used datastores before, only for this game because i’m very motivated to complete it

well it is easier to set it up than datastores

1 Like

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.

1 Like

Also couldn’t use just do

game.Players.PlayerAdded:Connect(function(Player)
local PlrStats = Instance.new("Folder")
PlrStats.Name = "PlrStats"
PlrStats.Parent = Player

local stand = Instance.new("StringValue")
Stand.Name = "Stand"
Stand.Value = 0
Stand.Parent = PlrStats

I think that’s a bit easier to work with.

1 Like

ok, trying right now. i’ll let you know

oof it didn’t work again, i’ll try and figure something else out

did you enabled the API service?

1 Like

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.

Why do you need to save the folder if a new folder is created and parented to the player when they join?

Remember that Datastores don’t work in Roblox Studio, have you been testing it in the actual game?

1 Like

Yes, I have. Doesn’t show there either

If you want to save everything inside the folder in the player’s backpack, you can simply loop through all tools for all players.

Players.PlayerRemoving:Connect(function(player)

for _Tool in pairs (player.Backpack.YOUR_FOLDER:GetChildren() do
-- Do stuff

This will help you out

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.

Then you could simply save the children of that folder. It doesn’t have to be tools.