Greetings, I’m trying to make a DataStore script but I’m having trouble.
Every DataStore script I’ve seen has just been of LeaderStats and not actual IntValue and BoolValues.
How would I make it so that a folder would be saved instead of just LeaderStats?
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local GameData = DataStoreService:GetDataStore("GameData")
Players.PlayerAdded:Connect(function(Player)
local PlayerDataFolder = Player.Data --< The folder I have set with all of the values
for i,v in pairs(PlayerDataFolder:GetDescendents()) do
if v:IsA("BoolValue") or v:IsA("IntValue") then
end
end
end)
Players.PlayerRemoving:Connect(function(Player)
end)
I have a beginning to it but I’m not sure how to do anything else because as I said, all of the scripts I’ve seen are just kills & money. Any help on this would be appreciated.
heres something that could help:
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local GameData = DataStoreService:GetDataStore("GameData")
Players.PlayerAdded:Connect(function(Player)
local PlayerDataFolder = Player.Data --< The folder I have set with all of the values
local ItemsToLoad = {} -- This will hold all the stats to save (basically a folder in a script)
for i,v in pairs(PlayerDataFolder:GetChildren()) do
if v:IsA("BoolValue") or v:IsA("IntValue") then
table.insert(ItemsToLoad, v) -- Put the value into a table to load
end
end
local PlayerId = Player.UserId -- The key for the player. We use player.Userid because a player can change there username and an id is the same forever
local Data
local Success = pcall(function()
Data = GameData:GetAsync(PlayerId) -- get the data from the player
end)
if Success and Data then -- if we got the data we will:
for i, value in pairs(ItemsToLoad) do
value.Value = Data[i] -- the index increases every time it loops
end
end
end)
Players.PlayerRemoving:Connect(function(Player)
-- we will do the same thing but save now
local PlayerDataFolder = Player.Data
local ItemsToSave = {}
local PlayerId = Player.UserId
for i, v in pairs(PlayerDataFolder:GetChildren()) do
if v:IsA("BoolValue") or v:IsA("IntValue") then
table.insert(ItemsToSave, v.Value) -- we will now get the value of the item
end
end
local Success, Error = pcall(function()
GameData:SetAsync(PlayerId, ItemsToSave)
end)
if Success then
print("Saved "..PlayerId) -- data saved!
elseif Error then
warn(Error) -- there was an error
end
end)
1 Like
Nope, that doesn’t seem to work.
It doesn’t seem to be printing anything and the data doesn’t save.
I changed the data folder location though because I forgot that sever scripts are unable to access the PlayerScripts folder because nothing was appearing in the Player.
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local GameData = DataStoreService:GetDataStore("GameData")
Players.PlayerAdded:Connect(function(Player)
local Character = Player.Character or Player.CharacterAdded:Wait()
local PlayerDataFolder = Character:WaitForChild("Data") --< The folder I have set with all of the values
local ItemsToLoad = {} -- This will hold all the stats to save (basically a folder in a script)
for i,v in pairs(PlayerDataFolder:GetChildren()) do
if v:IsA("BoolValue") or v:IsA("IntValue") then
table.insert(ItemsToLoad, v) -- Put the value into a table to load
end
end
local PlayerId = Player.UserId -- The key for the player. We use player.Userid because a player can change there username and an id is the same forever
local Data
local Success = pcall(function()
Data = GameData:GetAsync(PlayerId) -- get the data from the player
end)
if Success and Data then -- if we got the data we will:
for i, value in pairs(ItemsToLoad) do
value.Value = Data[i] -- the index increases every time it loops
end
end
end)
Players.PlayerRemoving:Connect(function(Player)
-- we will do the same thing but save now
local Character = Player.Character or Player.CharacterAdded:Wait()
local PlayerDataFolder = Character:WaitForChild("Data")
local ItemsToSave = {}
local PlayerId = Player.UserId
for i, v in pairs(PlayerDataFolder:GetChildren()) do
if v:IsA("BoolValue") or v:IsA("IntValue") then
table.insert(ItemsToSave, v.Value) -- we will now get the value of the item
end
end
local Success, Error = pcall(function()
GameData:SetAsync(PlayerId, ItemsToSave)
end)
if Success then
print("Saved "..PlayerId) -- data saved!
elseif Error then
warn(Error) -- there was an error
end
end)
Did I accidentally do something wrong?
There are also no errors in the console.