I want to save the time the player has been playing in a datastore but for some reason, it is not saving and I am getting a warning saying request throttled.
local DataStore = game:GetService("DataStoreService")
local time_ds = DataStore:GetDataStore("TimeDS1")
game.Players.PlayerAdded:Connect(function(player)
local leader = Instance.new("Folder")
leader.Name = "leaderstats"
leader.Parent = player
local time_played = Instance.new("IntValue")
time_played.Parent = leader
time_played.Name = "time_played"
time_played.Value = time_ds:GetAsync(tostring(player.UserId)) or 0
time_ds:SetAsync(tostring(player.UserId),time_played.Value)
end)
game.Players.PlayerRemoving:Connect(function(player)
print("Saving data...")
time_ds:SetAsync(tostring(player.UserId),player.leaderstats.time_played.Value)
print(player.UserId.."'s data has been saved!") -- Not getting to this part?
end)

5 comments
The parent argument to Instance.new is deprecated.
@incapaz ok I fixed that but I am still getting the same problem but for some reason sometimes it does save and some times it does not
OH the first argument to both Set and GetAsync is a string and you gave it a number, the UserId, so you'd do tostring(player.UserId)
@incapaz I updated my code in this question can you tell me if I did anything wrong because like I said it saves very little of the time and still getting request throttled
Well you shouldn't be SetAsync'ing once you create the Instances, only set async when they leave.