Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would i Save Folders using DataStores?

So i tried using this script for my game, no luck, ive been trying to make a JoJo game, but im having issues with saving stands,

local DataStore = game:GetService("DataStoreService")
local GameData = DataStore("Data")

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 has data")
  Stand.Value = saves
    else
  warn("No Data found")
 end
  
end)

game.Players.PlayersRemoving: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("Player Data Saved!")
end

3 comments

0

what does it print?

0

Doesnt print anything. when i reset, my "stand" folder in the players backpack disappears, so does the folder in workspace.

0

also the error i got from the script is "Data is not a valid member of DataStoreService"

2 answers

I think the problem is that you are calling the saveData function BEFORE it's created.The Script is read by the game from top to bottom, like this

function saveData(player,PlrStats)
	local stand = PlrStats:WaitForChild("Stand")
	GameData:SetASync(Player,UserId,stand.Value)
	warn("Player Data Saved")
end

game.Players.PlayersRemoving:Connect(function(Player)
	 local PlrStats = Player:WaitForChild("PlrStats")
	 saveData(Player,PlrStats)
end)

I don't think this is the problem tho

4 comments

0

and datastores don't save until api services are on and u are not in studio

0

no i have api enabled

0

you tried running DataStore() but its not a function thats why

0

how do i change it then

you need to do Datastore:GetDataStore(name) and not Datastore(name)

local DataStore = game:GetService("DataStoreService")
local GameData = DataStore:GetDataStore("Data")

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 has data")
  Stand.Value = saves
    else
  warn("No Data found")
 end

end)

game.Players.PlayersRemoving: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("Player Data Saved!")
end

This discussion is preserved as it was. New answers, comments, and votes are closed.