I'm really interested in datastores, and for my game i need to save multiple things. Tables came to my mind.
It did not work.
"Does anyone out there know how to save multiple data in one script?"
HERES MY FAILED ATTEMPT TO SAVE MULTIPLE DATA;
the only difference here is on line 35 and 36 and on line 50 and 51.
-- services -- local DataStoreService = game:GetService("DataStoreService") local SaveGameData = DataStoreService:GetDataStore("SaveGameData") -- variables local RemoteEvent = game.ReplicatedStorage.RemoteEvent -- leaderstats game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder", player) leaderstats.Name = "leaderstats" local SavedGames = Instance.new("IntValue", leaderstats) SavedGames.Name = "SavedGames" local OtherData = Instance.new("IntValue", leaderstats) OtherData.Name = "OtherData" -- getasync functions local PlayerUserId = "Player_"..player.UserId local Data local sucess, errormessage = pcall(function() Data = SaveGameData:GetAsync(PlayerUserId) end) -- setting data if sucess then SavedGames.Value = Data OtherData.Value = Data end end) -- when the player leaves do game.Players.PlayerRemoving:Connect(function(player) -- setasync functions local PlayerUserId = "Player_"..player.UserId local Data = { Games = player.leaderstats.SavedGames.Value; OtherData = player.leaderstats.SavedGames.Value; } local sucess, errormessage = pcall(function() SaveGameData:SetAsync(PlayerUserId, Data) end) -- if the data saved if sucess then print("Data saved sucessfully") else print("Data did not save because...") warn(errormessage) end end)
Hello. You need the name of the dictionary's value. For example, instead of writing SavedGames.Value = Data
you'd write SavedGames.Value = Data.Games
. Try this:
-- services -- local DataStoreService = game:GetService("DataStoreService") local SaveGameData = DataStoreService:GetDataStore("SaveGameData") -- variables local RemoteEvent = game.ReplicatedStorage.RemoteEvent -- leaderstats game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder", player) leaderstats.Name = "leaderstats" local SavedGames = Instance.new("IntValue", leaderstats) SavedGames.Name = "SavedGames" local OtherData = Instance.new("IntValue", leaderstats) OtherData.Name = "OtherData" -- getasync functions local PlayerUserId = "Player_"..player.UserId local Data local sucess, errormessage = pcall(function() Data = SaveGameData:GetAsync(PlayerUserId) end) -- setting data if sucess then if Data then SavedGames.Value = Data.Games OtherData.Value = Data.OtherData end else warn(errormessage) end end) -- when the player leaves do game.Players.PlayerRemoving:Connect(function(player) -- setasync functions local PlayerUserId = "Player_"..player.UserId local Data = { Games = player.leaderstats.SavedGames.Value; OtherData = player.leaderstats.SavedGames.Value; } local sucess, errormessage = pcall(function() SaveGameData:SetAsync(PlayerUserId, Data) end) -- if the data saved if sucess then print("Data saved sucessfully") else print("Data did not save because...") warn(errormessage) end end)
Please upvote and accept this answer if it helps.