I'm very new to datastores and I just created a script that saves the player's location. The script works, but sometimes it goes back to where they were a few seconds ago. Im getting an error in the output which I think may be the cause. The error says:
"DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 137521187"
Here is my code:
local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("LocationData")
local sessionData = {}
local function addPlayer(player)
local data = ds:GetAsync(player.UserId)
local char = player.Character or player.CharacterAdded:Wait()
local hrp = char:WaitForChild("HumanoidRootPart")
if not data then
sessionData[player] = { loc = { hrp.CFrame:GetComponents() } }
else
sessionData[player] = data
local cf = CFrame.new(table.unpack(sessionData[player].loc))
wait()
hrp.CFrame = cf
end
end
local function saveLocation(player)
local char = player.Character or player.CharacterAdded:Wait()
local hrp = char:WaitForChild("HumanoidRootPart")
sessionData[player].loc = { hrp.CFrame:GetComponents() }
ds:SetAsync(player.UserId, sessionData[player])
end
local function saveLoop()
while true do
wait(3)
for ply, v in pairs(sessionData) do
saveLocation(ply)
end
end
end
local function removePlayer(player)
saveLocation(player)
sessionData[player] = nil
end
game.Players.PlayerAdded:Connect(addPlayer)
game.Players.PlayerRemoving:Connect(removePlayer)
spawn(saveLoop)
