01 | local DataStore = game:GetService( "DataStoreService" ):GetDataStore( 'ToolSave' ) |
02 | game.Players.PlayerAdded:Connect( function (plr) |
03 | local savedstuff = DataStore:GetAsync(plr.userId) |
04 | if savedstuff ~ = nil then |
05 | for i,v in pairs (savedstuff) do |
06 | if game.ServerStorage.Inventory:FindFirstChild(v) ~ = nil then --Inventory is a folder with Weopons inside it |
07 | local weopon = game.ServerStorage.Inventory:FindFirstChild(v):Clone() |
08 | weopon.Parent = plr:WaitForChild( 'Backpack' ) |
09 |
10 | end |
11 | end |
12 | end |
13 | plr.CharacterRemoving:Connect( function () |
14 | plr.Character.Humanoid:UnequipTools() |
15 |
Players can change their name so using a name as a key is a bad idea. if a player changes their name when they rejoin your game it would make a new datastore for them as if they are a new player.
A better alternative is to use a players userId. every player is assigned a unique userId that never changes so you would be better off using it.
to use it is simply player.userId
and in your case,
1 | for i,v in pairs (plr.Backpack:GetChildren()) do |
2 | table.insert(Table,v.userId) --It uses name from what I understand |
3 | end |