Why does my datastore not save weapons when the player has the weapons equipped?

For some reason if you leave the game with a weapon equipped that weapon will not save and won’t be loaded in the next time you join.

 --} 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 uid = plr.userId;
 if overwrite then
  SaveData(plr, uid);
  
 else
  return;
 end;
end);
1 Like

Perhaps because the equipped weapon exists in the player’s character instead of Backpack.

Any idea on how to fix it? I paid someone to make it for me because i’ve never been good with datastores.

Well the part where you are adding the tools into the table which is going to be saved, you should also check if there is a tool in the player’s character and add that to the table as well.

you could either account for the tools in someone’s character, or unequip the tools by force with Humanoid’s UnequipTools method

2 Likes

Oh yeah I guess the unequipping would be a lot simpler.