Here’s a more efficient way of saving
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerInfo")
Players.PlayerAdded:Connect(function(player)
local UserId = player.UserId
local Key = "Player_".. UserId -- The DataKey
local InfoFolder = script.Info:Clone() -- Clones Info Folder into Player
InfoFolder.Parent = player
local Success, Ret
repeat
Success, Ret = pcall(DataStore.GetAsync, DataStore, Key)
if Success then
local PlayerInfo = Ret
for i, v in ipairs(InfoFolder:GetChildren()) do -- Grabs the values from the folder
if v then
if PlayerInfo then -- A check
if PlayerInfo[v.Name] then -- Checks if data store has a data with the same name as one of the values in the folder
v.Value = PlayerInfo[v.Name] -- Sets the value in that folder as the value set in the datastore
end
end
end
end
end
if Success then
warn("Success")
else
warn("Failed")
end
wait()
until Success
end)
Players.PlayerRemoving:Connect(function(player)
local UserId = player.UserId
local Key = "Player_".. UserId
local Success, Ret
repeat
local PlayerInfo = {} -- Table for data to be put in and saved
for i, v in ipairs(player.Info:GetChildren()) do -- Gets values from the value folder
if v then
PlayerInfo[v.Name] = v.Value -- Adds the name and value of the child in the folder to the table
end
end
Success, Ret = pcall(DataStore.SetAsync, DataStore, Key, PlayerInfo) -- Saves table (Dictionary basically)
if Success then
warn("Success")
else
warn("Failed")
end
wait()
until Success
end)
This datastore method allows you to place any IntValue or BoolValue or StringValue, etc. into the folder and it instantly becomes a variable that is saved. It’s all automatic.
