Why do you need to check that? What you need to prioritize is how to make your datastore script efficient in order for the warning to not appear. Also, where did you put that check?
in player.removing, when the last player left or shutdown it made 2 requests to the datastore, it works now tho with no warnings
Can you provide the most updated code? I wanna see what changes you did from otherās suggestion.
for sure
local DSS = game:GetService("DataStoreService")
local materialDS = DSS:GetDataStore("materialDS")
game.Players.PlayerAdded:Connect(function(player)
local materialFolder = Instance.new("Folder",player)
materialFolder.Name = "materialFolder"
local wood = Instance.new("IntValue",materialFolder)
wood.Value = 0
wood.Name = "wood"
local iron = Instance.new("IntValue",materialFolder)
iron.Value = 0
iron.Name = "iron"
local rock = Instance.new("IntValue",materialFolder)
rock.Value = 0
rock.Name = "rock"
local steel = Instance.new("IntValue",materialFolder)
steel.Value = 0
steel.Name = "steel"
local playerId = player.UserId
local data
local success, err = pcall(function()
data = materialDS:GetAsync(player.UserId)
end)
if success then
wood.Value = data.wood
steel.Value = data.steel
rock.Value = data.rock
iron.Value = data.iron
else
print("The player has no data!")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
if #game.Players:GetPlayers()> 1 then
local playerId = player.UserId
local statsTable = {
wood = player.materialFolder.wood.Value,
steel =player.materialFolder.steel.Value,
rock = player.materialFolder.rock.Value,
iron = player.materialFolder.steel.Value
}
local success,err = pcall(function()
materialDS:SetAsync(playerId,statsTable)
end)
if success then
print("data")
else
print("failed")
end
end
end)
game:BindToClose(function()
local players = game.Players:GetPlayers()
for i,player in pairs(players) do
local playerId = player.UserId
local statsTable = {
["Wood"]=player.materialFolder.wood.Value,
["Steel"]=player.materialFolder.steel.Value,
["Rock"] = player.materialFolder.rock.Value,
["Iron"]=player.materialFolder.steel.Value
}
local success,err = pcall(function()
materialDS:SetAsync(playerId,statsTable)
end)
if success then
print("data saved")
else
print("failed")
end
end
end)
I believe that it saves twice when the player leaves because the Player.Removing and game:BindToClose() both trigger. You can only save to datastore every 6 seconds or so, causing either Player.Removing or game:BindToCLose() to save, while the other one gives you that error.
I donāt think you need to add the check if the player count is greater than 1 because when you are testing it in Studio, once you leave it automatically closes the local server as well.
Because of this, there is a very small interval between PlayerRemoving and BindToClose. I donāt think this warning will pop up on a live server.
Yea i figured it out, posted the updated code above