I have tried debugging and I found out that the thing that is not working is is the :setasync part
datastore script:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
print("finished creating leaderstats")
local data
local success, errormessage = pcall(function()
data = DataStore:GetAsync(player.UserId)
end)
print("finish loading data")
if success then
cash.Value = data
end
print("done with player added script")
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = {player.leaderstats.Cash.Value}
print("defined the data")
local success, errormessage = pcall(function()
DataStore:SetAsync(player.UserId, data )--part where it goes wrong
end)
print("saved the data ")
if success then
print("we saved the data")
else
print("there is a error")
warn(errormessage)
end
end)
However now I got a new problem in which the error says
DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 1503729675
I know this means that there is too much requests but I don’t know how to fix it
new script
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
print("finished creating leaderstats")
local data
local success, errormessage = pcall(function()
data = DataStore:GetAsync(player.UserId)
end)
print("finish loading data")
if success then
cash.Value = data
end
print("done with player added script")
end)
local data
game.Players.PlayerRemoving:Connect(function(player)
data = {player.leaderstats.Cash.Value}
print("defined the data")
local success, errormessage = pcall(function()
DataStore:SetAsync(player.UserId, data)
end)
print("saved the data ")
if success then
print("we saved it")
else
print("there was a error")
warn(errormessage)
end
end)
game:BindToClose(function()
local success, errormessage = pcall(function()
for i, player in pairs(game.Players:GetPlayers()) do
DataStore:SetAsync(player.UserId, data)
end
end)
print("saved the data ")
if success then
print("we saved it")
else
warn(errormessage)
end
end)