Datastore not saving weapons?

 --} 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.

1 Like

This line in the PlayerRemoving event seems pretty suspect.

local char = game.Workspace:FindFirstChild(plr.Name)
char.Humanoid:UnequipTools()

I don’t think the character will always exist when the player removing event fires and this seems like it could cause problems. I would recommend changing it to

local char = plr.Character
if char then
  local humanoid = char:FindFirstChild("Humanoid")
  if humanoid then
    humanoid:UnequipTools()
  end
end

Other than that I would recommend adding a few print statements at various points to try to track down where things are going wrong.

2 Likes

Another thing I noticed is in the SaveData function you create a variable for the StarterGear and then do not use it, maybe that is a potential source of the issue depending on how your system is set up?

1 Like

I fixed it by making it save StarterGear instead of Backpack but thank you.