The script is just a equip - unequip syncronization with the server
When players clicking the buttons to equip/unequip items, then Im recieving many of the warns “DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests”
I’ve tried using the version below, but its not saving the data, so i modified the script to the second variant and its worked for me, but as i wrote, “DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests” errors.
local DSS = game:GetService("DataStoreService")
local players = game:GetService("Players")
local datastoreName = "test"
local DB = DSS:GetDataStore(datastoreName)
local sessionData = {}
local equipTowerFunction = workspace.ChangeTower
local players = game:GetService("Players")
local function PrintAllDataOfPlayer(player)
-- Loop through the saved data table and create a new folder for each key-value pair
for key, value in pairs(sessionData[player.UserId]) do
print(key,value)
end
end
local function PrintValueDataOfPlayer(player, valueName)
-- Loop through the saved data table and create a new folder for each key-value pair
for key, value in pairs(sessionData[player.UserId]) do
if key == valueName then
print(key,value)
end
end
end
local function updateDataStoreValue(datastoreName, player, valueName, newValue)
-- Get the datastore using its name
local dataStore = DSS:GetDataStore(datastoreName)
-- Call UpdateAsync to update the value of the key for the player's UserId
local success, value = pcall(function()
return dataStore:UpdateAsync(player.UserId, function(oldValue)
-- Merge the old value with the new value
local updatedValue = oldValue or {}
updatedValue[valueName] = newValue
-- Set the new value
return updatedValue
end)
end)
-- Check if the update was successful or not
if success then
print("Value "..valueName.." updated to: " .. tostring(value))
print("Now it is:")
PrintValueDataOfPlayer(player, valueName)
else
warn("Error updating value: " .. tostring(value))
end
end
local tryEquip = function(player, slotName, towerName)
--players:FindFirstChild(tostring(player)):FindFirstChild("StuffToSave")[tostring(slotName)].Value = towerName
--надо на серваке изменять и синхронизировать всё
updateDataStoreValue(datastoreName, player, slotName, towerName)
print("Function Datastore Updated", slotName, towerName)
SyncWithDatabase(player)
return true
end
equipTowerFunction.OnServerInvoke = tryEquip
local function GetNewPlayersData()
local SS = game:GetService("ServerStorage")
local dataFolder = SS.StuffToSave
local data = {}
for i, v in pairs(dataFolder:GetChildren()) do
data[v.Name] = v.Value
end
return data
end
local function GetPlayerDataOnServer(player)
local save = player:WaitForChild("StuffToSave")
local data = {}
for i, v in pairs(save:GetChildren()) do
data[v.Name] = v.Value
end
return data
end
function SyncWithDatabase(player)
local oldData = player:FindFirstChild("StuffToSave")
if oldData == nil then
local dataFolder = Instance.new("Folder")
dataFolder.Name = "StuffToSave"
dataFolder.Parent = player
-- Loop through the saved data table and create a new folder for each key-value pair
for key, value in pairs(sessionData[player.UserId]) do
-- Create a new IntValue instance to store the value
local newValue = nil
if type(value) == "number" then
newValue = Instance.new("IntValue")
else
newValue = Instance.new("StringValue")
end
newValue.Value = value
newValue.Name = tostring(key)
newValue.Parent = dataFolder
end
else
--if player have existing data folder
-- Loop through the saved data table and create a new folder for each key-value pair
for key, value in pairs(sessionData[player.UserId]) do
-- Change the existing value
local newValue = oldData:FindFirstChild(tostring(key))
newValue.Value = value
end
print("synced the data with database and player values in stufftosave")
end
end
local function PlayerAddedLoadData(player)
local success = nil
local playerData = nil
local attempt = 1
repeat
success, playerData = pcall(function()
return DB:GetAsync(player.UserId)
end)
attempt = attempt + 1
if not success then
warn(playerData)
task.wait(3)
end
until success or attempt == 5
if success then
print("Connected to database for"..player.Name)
if not playerData then
print("Assigning data to player", player.Name.."...")
playerData = GetNewPlayersData()
end
sessionData[player.UserId] = playerData
SyncWithDatabase(player)
local loaded = Instance.new("BoolValue")
loaded.Name = "LOADED"
loaded.Parent = player
else
warn("Failed to load data for"..player.Name)
player:Kick("Unable to load your data. Try again later!")
end
end
local function PlayerLeavingSave(player)
if sessionData[player.UserId] then
local success = nil
local errorMessage = nil
local attempt = 1
repeat
success, errorMessage = pcall(function()
DB:SetAsync(player.UserId, sessionData[player.UserId])
end)
attempt = attempt + 1
if not success then
warn(errorMessage)
task.wait(3)
end
until success or attempt == 5
if success then
print("Data saved for"..player.Name)
else
print("Failed to save data for"..player.Name)
end
end
end
players.PlayerAdded:Connect(PlayerAddedLoadData)
players.PlayerRemoving:Connect(PlayerLeavingSave)
The script that im using now:
local DSS = game:GetService("DataStoreService")
local players = game:GetService("Players")
local datastoreName = "test2"
local DB = DSS:GetDataStore(datastoreName)
local sessionData = {}
local equipTowerFunction = workspace.ChangeTower
local unequipTowerFunction = workspace.UnequipTower
local players = game:GetService("Players")
local function PrintAllDataOfPlayer(player)
-- Loop through the saved data table and create a new folder for each key-value pair
for key, value in pairs(sessionData[player.UserId]) do
print(key,value)
end
end
local function PrintValueDataOfPlayer(player, valueName)
-- Loop through the saved data table and create a new folder for each key-value pair
for key, value in pairs(sessionData[player.UserId]) do
if key == valueName then
print(key,value)
end
end
end
local function updateDataStoreValue(player, valueName, newValue)
local serverData = GetPlayerDataOnServer(player)
serverData[tostring(valueName)] = newValue
print(serverData)
local success = nil
local errorMessage = nil
local attempt = 1
repeat
success, errorMessage = pcall(function()
DB:SetAsync(player.UserId, serverData)
end)
attempt = attempt + 1
if not success then
warn(errorMessage)
task.wait(3)
end
until success or attempt == 5
if success then
print("Data saved for"..player.Name)
else
print("Failed to save data for"..player.Name)
end
LoadData(player)
end
local tryEquip = function(player, slotName, towerName)
updateDataStoreValue(player, slotName, towerName)
return true
end
local unEquip = function(player, slotName)
updateDataStoreValue(player, slotName, "Nothing")
return true
end
function GetNewPlayersData()
local SS = game:GetService("ServerStorage")
local dataFolder = SS.StuffToSave
local data = {}
for i, v in pairs(dataFolder:GetChildren()) do
data[v.Name] = v.Value
end
return data
end
function GetPlayerDataOnServer(player)
local save = player:WaitForChild("StuffToSave")
local data = {}
for i, v in pairs(save:GetChildren()) do
data[v.Name] = v.Value
end
return data
end
function SyncWithDatabase(player)
local oldData = player:FindFirstChild("StuffToSave")
if oldData == nil then
local dataFolder = Instance.new("Folder")
dataFolder.Name = "StuffToSave"
dataFolder.Parent = player
-- Loop through the saved data table and create a new folder for each key-value pair
for key, value in pairs(sessionData[player.UserId]) do
-- Create a new IntValue instance to store the value
local newValue = nil
if type(value) == "number" then
newValue = Instance.new("IntValue")
else
newValue = Instance.new("StringValue")
end
newValue.Value = value
newValue.Name = tostring(key)
newValue.Parent = dataFolder
end
else
--if player have existing data folder
-- Loop through the saved data table and create a new folder for each key-value pair
for key, value in pairs(sessionData[player.UserId]) do
-- Change the existing value
local newValue = oldData:FindFirstChild(tostring(key))
newValue.Value = value
end
print("synced the data with database and player values in stufftosave")
end
end
function LoadData(player)
local success = nil
local playerData = nil
local attempt = 1
repeat
success, playerData = pcall(function()
return DB:GetAsync(player.UserId)
end)
attempt = attempt + 1
if not success then
warn(playerData)
task.wait(3)
end
until success or attempt == 5
if success then
print("Connected to database for"..player.Name)
if not playerData then
print("Assigning data to new player", player.Name.."...")
playerData = GetNewPlayersData()
end
sessionData[player.UserId] = playerData
SyncWithDatabase(player)
local loaded = Instance.new("BoolValue")
loaded.Name = "LOADED"
loaded.Parent = player
else
warn("Failed to load data for"..player.Name)
player:Kick("Unable to load your data. Try again later!")
end
end
function PlayerLeavingSave(player)
if sessionData[player.UserId] then
local success = nil
local errorMessage = nil
local attempt = 1
repeat
success, errorMessage = pcall(function()
DB:SetAsync(player.UserId, sessionData[player.UserId])
end)
attempt = attempt + 1
if not success then
warn(errorMessage)
task.wait(3)
end
until success or attempt == 5
if success then
print("Data saved for"..player.Name)
else
print("Failed to save data for"..player.Name)
end
end
end
--saves
players.PlayerAdded:Connect(LoadData)
players.PlayerRemoving:Connect(PlayerLeavingSave)
--events remote functions
equipTowerFunction.OnServerInvoke = tryEquip
unequipTowerFunction.OnServerInvoke = unEquip
I would be very grateful if you could tell me what the problem is and advise me how to improve the script in terms of performance