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

0

The parent argument to Instance.new is deprecated.

0

@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

0

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)

0

@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

0

Well you shouldn't be SetAsync'ing once you create the Instances, only set async when they leave.