--} Reference services:
local RS = game:GetService("ReplicatedStorage");
local DSS = game:GetService("DataStoreService");
local P = game:GetService("Players");
--} Reference variables:
local bytes = {
{97, 122}; --! a-z:
{65, 90}; --! A-Z:
{48, 57}; --! 0-9:
};
local overwrite = true;
local vault = DSS:GetDataStore("Test");
--} Reference functions:
function GetRandomCharacter()
local charset = bytes[math.random(1, 3)];
return string.char(math.random(charset[1], charset[2]));
end;
function GenerateID(len)
local id;
for i = 1, len do
local char = GetRandomCharacter();
if not id then
id = char;
else
id = id .. char;
end;
end;
return id;
end;
function SaveData(plr, uid)
local pack = plr:WaitForChild("Backpack");
local gear = plr:WaitForChild("StarterGear");
local keys = {};
if #pack:GetChildren() > 0 then
for i, v in next, pack:GetChildren() do
keys[GenerateID(20)] = v.Name;
end;
end;
vault:SetAsync(uid, keys);
end;
function LoadData(plr, uid)
local pack = plr:WaitForChild("Backpack");
local gear = plr:WaitForChild("StarterGear");
for i, v in next, vault:GetAsync(uid) do
for x, y in next, RS:GetChildren() do
if v == y.Name then
y:Clone().Parent = pack;
y:Clone().Parent = gear;
end;
end;
end;
end;
P.PlayerAdded:connect(function(plr)
local uid = plr.userId;
if vault:GetAsync(uid) then
LoadData(plr, uid);
print("Worked - game.Workspace.Data Store")
else
return;
end;
end);
P.PlayerRemoving:connect(function(plr)
local char = game.Workspace:FindFirstChild(plr.Name)
char.Humanoid:UnequipTools()
local uid = plr.userId;
if overwrite then
SaveData(plr, uid);
else
return;
end;
end);
It has no errors.