I tried to make a queue where the save waits for its turn but they both get activated with the same queue then results in a "Request was throttled." This event can be activated many times in a few seconds so that's where the idea of a line to wait to be saved came from.
The script -
-- Variables
local xpDataStore = game:GetService('DataStoreService'):GetDataStore('xpStore')
local lvlDataStore = game:GetService('DataStoreService'):GetDataStore('lvlStore')
local repStorage = game:GetService('ReplicatedStorage')
local xpChanger = repStorage:WaitForChild('XpChangedEvent')
local xpToAdd = 5
local part = script.Parent
local maxLvl = 350
local canSave = true
local queue = 6
-- Events
pcall(part.ClickDetector.MouseClick:Connect(function(plr)
local key = 'Player-'..plr.userId
local curLvl = lvlDataStore:GetAsync(key)
local curXp = xpDataStore:GetAsync(key)
print('serverCurXp = '..curXp)
-- Fire to player
xpChanger:FireClient(plr,xpToAdd)
-- Math
local newLvl = curLvl + 1
local newXp = curXp + xpToAdd
local lvlEquation = 10*curLvl
print('NewXp = '..newXp)
-- Adds lvls and sets xp
if curLvl == 0 and newXp >= 10 then
if canSave then
queue = 10
canSave = false
print('Cant SAVE NOW')
wait(6)
print ('1st lvl')
lvlDataStore:SetAsync(key,(newLvl))
xpDataStore:SetAsync(key,(newXp - lvlEquation))
canSave = true
else
wait(10 + queue)
queue = queue + queue
print('Cant SAVE NOW')
wait(6)
print ('1st lvl')
lvlDataStore:SetAsync(key,(newLvl))
xpDataStore:SetAsync(key,(newXp - lvlEquation))
canSave = true
end
elseif curLvl >= maxLvl then
if canSave then
queue = 10
canSave = false
print('Cant SAVE NOW')
wait(6)
print('max lvl')
lvlDataStore:SetAsync(key,(350))
xpDataStore:SetAsync(key,(0))
canSave = true
queue = 0
else
wait(10 + queue)
queue = queue + queue
print('Cant SAVE NOW')
wait(6)
print('max lvl')
lvlDataStore:SetAsync(key,(350))
xpDataStore:SetAsync(key,(0))
canSave = true
end
elseif lvlEquation <= newXp and lvlEquation ~= 0 then
if canSave then
queue = 10
canSave = false
print('Cant SAVE NOW')
wait(6)
print('lvl up')
lvlDataStore:SetAsync(key,(newLvl))
xpDataStore:SetAsync(key,(newXp - lvlEquation))
canSave = true
queue = 0
else
wait(10 + queue)
queue = queue + queue
print('Cant SAVE NOW')
wait(6)
print('lvl up')
lvlDataStore:SetAsync(key,(newLvl))
xpDataStore:SetAsync(key,(newXp - lvlEquation))
canSave = true
end
else
if canSave then
queue = 10
canSave = false
print('Cant SAVE NOW')
wait(6)
print('no lvl')
lvlDataStore:SetAsync(key,(curLvl))
xpDataStore:SetAsync(key,(newXp))
canSave = true
queue = 0
else
wait(10 + queue)
queue = queue + queue
canSave = false
print('Cant SAVE NOW')
wait(6)
print('no lvl')
lvlDataStore:SetAsync(key,(curLvl))
xpDataStore:SetAsync(key,(newXp))
canSave = true
end
end
end))

3 comments
userId is deprecated use UserId
And you don't need separate data stores just store a table lol
so I just do game:GetService('DataStoreService'):GetDataStore('xpStore','lvlStore')