Hi DevForum,
The title is pretty self-explanatory, but I was wondering with the code I made, if it was efficient in saving multiple values. If not, can you help me correct any mistakes?
Thanks!
Code:
local NamesModule = require(script.AvailabilitiesModule)
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local playerData = Instance.new("Folder", player)
playerData.Name = "playerData"
local family = Instance.new("StringValue", playerData)
family.Name = "Family"
local jenny = Instance.new("IntValue", playerData)
jenny.Name = "Jenny"
jenny.Value = 0
family.Value = NamesModule[math.random(1,#NamesModule)]
local fullname = (player.Name.. " ".. family.Value)
local UserId = "Player_".. player.UserId
--// Load Data
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(UserId)
end)
if success then
family.Value = data.family
jenny.Value = data.jenny
--// Set data = the values
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local UserId = "Player_".. player.UserId
local data = {family = player.playerData.Family.Value, jenny = player.playerData.Jenny.Value}
local success, errormessage = pcall(function()
myDataStore:SetAsync(UserId, data)
end)
if success then
print("Data Successfully Saved!")
else
warn("There was an error saving your data. ErrorMessage: ".. errormessage)
end
end)
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
local UserId = "Player_".. player.UserId
local data = {family = player.playerData.Family.Value, jenny = player.playerData.Jenny.Value}
myDataStore:SetAsync(UserId, data)
end
end)