I’m trying to set datastore values in a script, and when the function runs it errors hundreds of times. Here’s the script:
game.Players.PlayerAdded:Connect(function (Player)
local Stats = script.Stats:Clone()
Stats.Parent = Player
local function UpdateStats()
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerStats")
local Data = {}
local SetSuccess, SetError = pcall(function()
DataStore:SetAsync(Player.UserId.."_Money", Stats.Money.Value)
DataStore:SetAsync(Player.UserId.."_CapacityLevel", Stats.CapacityLevel.Value)
DataStore:SetAsync(Player.UserId.."_AmountLevel", Stats.AmountLevel.Value)
DataStore:SetAsync(Player.UserId.."_WalkspeedLevel", Stats.WalkspeedLevel.Value)
end)
if not SetSuccess then
warn(SetError)
game.ReplicatedStorage.Notify:FireClient(Player, "Warning: Failed to save data, screenshot this and your stats. "..SetError, Color3.fromRGB(200, 150, 50))
end
task.wait()
local GetSuccess, GetError = pcall(function()
Data.Money = DataStore:GetAsync(Player.UserId.."_Money")
Data.CapacityLevel = DataStore:GetAsync(Player.UserId.."_CapacityLevel")
Data.AmountLevel = DataStore:GetAsync(Player.UserId.."_AmountLevel")
Data.WalkspeedLevel = DataStore:GetAsync(Player.UserId.."_WalkspeedLevel")
end)
if not GetSuccess then
warn(GetError)
game.ReplicatedStorage.Notify:FireClient(Player, "Warning: Failed to retrieve data, screenshot this and your stats. "..GetError, Color3.fromRGB(200, 150, 50))
end
Stats.Money.Value = Data.Money
Stats.CapacityLevel.Value = Data.CapacityLevel
Stats.AmountLevel.Value = Data.AmountLevel
Stats.WalkspeedLevel.Value = Data.WalkspeedLevel
Player.Character.Humanoid.WalkSpeed = 15 + Stats.WalkspeedLevel.Value
end
Player.CharacterAdded:Connect(UpdateStats)
for _, Stat in pairs(Stats:GetChildren()) do
Stat.Changed:Connect(UpdateStats)
end
UpdateStats()
end)
And this is the error(s) I’m getting:
302: SetAsync request dropped. Request was throttled, but throttled request queue was full.
DataStoreService: SetAsyncThrottle: SetAsync request dropped. Request was throttled, but throttled request queue was full. API: SetAsync, Data Store: PlayerStats
How do I fix this? Am I doing something wrong?