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)

6 comments
This will work, but you should also add an if statement to see if they actually have data when trying to load, because if a new player joins the game and they don't have any data, it will error.
Yes, do that too.
Okay, thanks for replying!
is it common that it does not work in studio? and i have got API on.
Yes, it is common.
Okay, thanks! No wonder you have so much reputation.