How can I redo this DataStore, because sometimes players lose their weapons.
local DataStore = game:GetService("DataStoreService"):GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(plr)
local data
local success, errorMessage = pcall(function()
data = DataStore:GetAsync(plr.UserId)
end)
if data ~= nil then
for _, toolName in pairs(data) do
local tool = game.ReplicatedStorage.Tools:FindFirstChild(toolName)
if tool then
local newTool = tool:Clone()
newTool.Parent = plr.Backpack
local newTool = tool:Clone()
newTool.Parent = plr.StarterGear
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local toolsTable = {}
for _, tool in pairs(plr.Backpack:GetChildren()) do
if game.ReplicatedStorage.Tools:FindFirstChild(tool.Name) then
table.insert(toolsTable,tool.Name)
end
end
local success, errorMessage = pcall(function()
DataStore:SetAsync(plr.UserId,toolsTable)
end)
end)
game:BindToClose(function()
for _, plr in pairs(game.Players:GetPlayers()) do
local toolsTable = {}
for _, tool in pairs(plr.Backpack:GetChildren()) do
if game.ReplicatedStorage.Tools:FindFirstChild(tool.Name) then
table.insert(toolsTable,tool.Name)
end
end
local success, errorMessage = pcall(function()
DataStore:SetAsync(plr.UserId,toolsTable)
end)
end
end)
Well first of all your saving the tools in the player’s backpack even if the player’s SetAsync() request failed.
When a setasync() request fails and the player doesn’t get the items loaded you still save it to the datastore, meaning player will loose ALL his items if his data isn’t loaded.
What a better idea would be is tagging the player who didn’t get his data-loaded with ‘DataDidNotLoad’ and when the player leaves and has that tag don’t SetAsync().
Okay Thanks! I have another question though. My game just reached 1.27 visits and I know the game gets quite boring but It is really fun still at the same time would you want to try it?