So if players join at the same time an error comes up saying data queue, for a very long time until the player actual loads in the data.
--Services--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
--Shortcuts--
local Folder = ReplicatedStorage:WaitForChild("DataStore")
--Variables--
--functions--
local function GetDS(Value, Player)
local DS = DataStoreService:GetDataStore(Value.Name)
local Data;
local DataFetchSuccess, ErrorMessage = pcall(function()
Data = DS:GetAsync(tostring(Player.UserId))
end)
if DataFetchSuccess then
if Data == nil and Value.Name == "DeafultKnife" or Value.Name == "DeafultFlashlight" or Value.Name == "Clown" then
Value.Value = true
elseif Data == nil and Value.Name == "Skin" then
Value.Value = "Clown"
elseif Data == nil and Value.ClassName == "StringValue" then
Value.Value = ("Deafult"..Value.Name)
elseif Data == nil and Value.ClassName == "BoolValue" then
Value.Value = false
elseif Data == nil and Value.ClassName == "NumberValue" then
Value.Value = 0
else
Value.Value = Data
end
end
end
local function SetDS(Value, Player)
if Player.CanSaveData.Value == false then return end
local DS = DataStoreService:GetDataStore(Value.Name)
local Data = DS:GetAsync(tostring(Player.UserId))
if Data ~= Value.Value then
local DataWriteSuccess, ErrorMessage = pcall(function()
DS:SetAsync(tostring(Player.UserId),Value.Value)
end)
if not DataWriteSuccess then
local Retry = 0
while Retry < 6 do
wait(60)
local Succeded, Error = pcall(function()
DS:SetAsync(tostring(Player.UserId),Value.Value)
end)
if Succeded then break end
Retry = Retry + 1
end
end
end
end
local function CheckChildren(Key, Children, Player)
for i = 1, #Children do
if Children[i].ClassName == "Folder" then
local C = Children[i]:GetChildren()
if C then
CheckChildren(Key, C, Player)
end
else
if Key == "GetDS" then
GetDS(Children[i], Player)
else
SetDS(Children[i], Player)
end
end
end
end
game.Players.PlayerAdded:Connect(function(Player)
--//Loading DataStores\\--
local Clone = Folder:Clone()
Clone.Parent = Player
local FolderChildren = Clone:GetChildren()
CheckChildren("GetDS", FolderChildren, Player)
-------------------
print("DataStore Loaded")
--CanSaveValue--
local CanSave = Instance.new("BoolValue",Player)
CanSave.Name = "CanSaveData"
CanSave.Value = true
end)
game.Players.PlayerRemoving:Connect(function(Player)
local Clone = Player:FindFirstChild("DataStore")
if Clone then
local CloneChildren = Clone:GetChildren()
CheckChildren("SetDS", CloneChildren, Player)
print("Inventory Saved")
end
end)