I need help, i have this datastore code by gnomecode but it only supports one instance.
if i wanna add like another value to save it wouldn’t save. how can i modify the script to save?
local players = game:GetService("Players")
local RunService = game:GetService("RunService")
local DatastoreService = game:GetService("DataStoreService")
local database = DatastoreService:GetDataStore("data")
local sessionData = {}
function playerAdded(player)
local PlayerData = Instance.new("Folder")
PlayerData.Name = "plrData"
local FOV = Instance.new("IntValue")
FOV.Name = "FOV"
FOV.Parent = PlayerData
local success = nil
local playerData = nil
local attempt = 1
repeat
success, playerData = pcall(function()
return database:GetAsync(player.UserId)
end)
attempt += 1
if not success then
warn(playerData)
task.wait(3)
end
until success or attempt == 5
if success then
print("Connected to database")
if not playerData then
print("Assigning default data")
playerData = {
["FOV"] = 70,
}
end
sessionData[player.UserId] = playerData
else
warn("Unable to get data for", player.Name)
player:Kick("Unable to load your data, try again later.")
end
FOV.Value = sessionData[player.UserId].FOV
FOV.Changed:Connect(function()
sessionData[player.UserId].FOV = FOV.Value
end)
PlayerData.Parent = player
end
players.PlayerAdded:Connect(playerAdded)
function PlayerLeaving(player)
if sessionData[player.UserId] then
local success = nil
local errorMsg = nil
local attempt = 1
repeat
success, errorMsg = pcall(function()
database:SetAsync(player.UserId, sessionData[player.UserId])
end)
attempt += 1
if not success then
warn(errorMsg)
task.wait(3)
end
until success or attempt == 5
if success then
print("Data saved for", player.Name)
else
warn("Unable to save for", player.Name)
end
end
end
players.PlayerRemoving:Connect(PlayerLeaving)
function ServerShutdown()
if RunService:IsStudio() then
return
end
for i, player in ipairs(players:GetPlayers()) do
task.spawn(function()
PlayerLeaving(player)
print("Shutting down, data saving")
end)
end
end
game:BindToClose(ServerShutdown)