so I added a model in the pets folder, what the script should do is save it and print the name, but instead it only prints blank!
local dts = game:GetService("DataStoreService")
local petsData = dts:GetDataStore("Pets")
local pets = game.ReplicatedStorage.pets
local petNames = {}
local function SavePlayerData(Player)
local Pets = Player:WaitForChild("Pets")
if Pets then
local success, errormsg = pcall(function()
for _, child in pairs(Pets:GetDescendants()) do
table.insert(petNames, child.Name)
end
petsData:SetAsync(Player.UserId, petNames)
end)
if not success then
return errormsg
end
end
end
game.Players.PlayerAdded:Connect(function(Player)
local clone = script.Pets:Clone()
clone.Parent = Player
for i = 1, 100 do
print(petNames, i)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local errormsg = SavePlayerData(player)
if errormsg then
warn(errormsg)
end
wait(2)
end)
game:BindToClose(function()
for i,player in pairs(game.Players:GetPlayers()) do
local errormsg = SavePlayerData(player)
if errormsg then
warn(errormsg)
end
end
wait(2)
end)
`local dts = game:GetService("DataStoreService")
local petsData = dts:GetDataStore("Pets")
local pets = game.ReplicatedStorage.pets
local function SavePlayerData(Player)
local Pets = Player:WaitForChild("Pets")
if Pets then
local success, errormsg = pcall(function()
local petNames = {}
for _, child in pairs(Pets:GetDescendants()) do
table.insert(petNames, child.Name)
end
petsData:SetAsync(Player.UserId, petNames)
end)
if not success then
return errormsg
end
end
end
game.Players.PlayerAdded:Connect(function(Player)
local clone = script.Pets:Clone()
clone.Parent = Player
for i = 1, 100 do
local petNames = {}
petsData:GetAsync(Player.UserId, petNames)
print(petNames, i)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local errormsg = SavePlayerData(player)
if errormsg then
warn(errormsg)
end
wait(2)
end)
game:BindToClose(function()
for i,player in pairs(game.Players:GetPlayers()) do
local errormsg = SavePlayerData(player)
if errormsg then
warn(errormsg)
end
end
wait(2)
end)`
local clone = script.Pets:Clone()
clone.Parent = Player
local petNames
local succ, err = pcall(function()
petNames = petsData:GetAsync(Player.UserId) or {}
end)
print(petNames)
local function SavePlayerData(Player)
local Pets = Player:WaitForChild("Pets")
if Pets then
local success, errormsg = pcall(function()
for _, child in pairs(Pets:GetDescendants()) do
table.insert(petNames, child.Name)
end
petsData:SetAsync(Player.UserId, petNames)
end)
if not success then
return errormsg
end
end
end
I feel like there’s a lot of easier and simpler ways to save data that people don’t really see or use. You don’t need modules or some complicated jumble of a mess really. All you have to do is use
local PetsDS = DSS:GetData(“DataStoreName”)
Then when player joins:
local Pets = PetsDS:GetAsync(player.UserId)
for i,v in pairs(Pets) do
—Whatever here
end
And whenever you save it just make sure you save it in a table format of {Pet1,Pet2,…}
local HTTPService = game:GetService("HTTPService")
local function SavePlayerData(Player)
local Pets = Player:WaitForChild("Pets")
if Pets then
local success, errormsg = pcall(function()
for _, child in pairs(Pets:GetDescendants()) do
table.insert(petNames, child.Name)
end
warn(petNames)
petsData:SetAsync(Player.UserId, HTTPService:JSONEncode(petNames))
end)
if not success then
return errormsg
end
end
end
then you need to change it back so:
local clone = script.Pets:Clone()
clone.Parent = Player
local petNames
local succ, err = pcall(function()
petNames = HTTPService:JSONDecode(petsData:GetAsync(Player.UserId) or "{}")
end)
print(petNames)
The data that is being saved will be outputed in console as orange text, and the data being loaded will be outputed in white text