So I made a basic datastore script which basically saves string values (there are multiple of them) and when I leave in studio, it freezes/hangs for 5-20 seconds then closes. I get this warning:
Script:
game:GetService("Players").PlayerRemoving:Connect(function(Player)
pcall(function()
local DataKey = Player.UserId.."-save"
local Binds = Player:WaitForChild("Keybinds")
local Keys = {}
for _, Index in pairs(Binds:GetChildren()) do
if Index:IsA("Folder") then
Keys[Index.Name] = {}
for i, values in pairs(Index:GetChildren()) do
if values:IsA("StringValue") then
Keys[Index.Name][values.Name] = values.Value
DataModule.SaveData(DataKey, BindDataStore, Keys)
end
end
end
end
end)
end)
Module:
local DataSaver = {}
function DataSaver.SaveData(key, datastore, data)
datastore:SetAsync(key, data)
end
function DataSaver.LoadData(key, datastore)
local data = datastore:GetAsync(key)
return data
end
return DataSaver
Instead of making multiple save requests for each different value, you should group all the values in just one table and just use one save request. Same can be done for fetching data too. There is a limit to how many datastore requests you can send in one minute per server.
for _, Index in pairs(Binds:GetChildren()) do
if Index:IsA("Folder") then
Keys[Index.Name] = {}
for i, values in pairs(Index:GetChildren()) do
if values:IsA("StringValue") then
Keys[Index.Name][values.Name] = values.Value
DataModule.SaveData(DataKey, BindDataStore, Keys)
end
end
end
end
game:GetService("Players").PlayerRemoving:Connect(function(Player)
pcall(function()
local DataKey = Player.UserId.."-save"
local Binds = Player:WaitForChild("Keybinds")
local Keys = {}
for _, Index in pairs(Binds:GetChildren()) do
if Index:IsA("Folder") then
Keys[Index.Name] = {}
for i, values in pairs(Index:GetChildren()) do
if values:IsA("StringValue") then
Keys[Index.Name][values.Name] = values.Value
DataModule.SaveData(DataKey, BindDataStore, Keys)
end
end
end
end
DataModule.SaveData(DataKey, BindDataStore, Keys)
end)
end)