Hello, I know enough to know what I don't know. I have a DataStore that saves leaderstats just fine, a Shop that adds items to the players Backpack and StarterPack. I'm having difficulty with getting the weapons to save after they player has died. I don't see any tutorials online about how to do this, and "look at the wiki" just confuses me. I know the most common answer I see when other people ask on Youtube or the Forums is, "add a boolValue to the player that gets enabled when a weapon is purchased, and have the datastore check that."
Okay, here's what I've done, for the purpose of trying to keep an "M9" with the player after they purchase the item from the Shop and are killed/respawn.
1. I added a Bool Value under Players labeled "HasM9"
2. I added a line to the purchase script for the M9 in my shop that sets the Players Bool Value to true:
-- Shop purchase code above, Adding item to Backpack/Starter Gear below:
tool:Clone().Parent = plr.Backpack
tool:Clone().Parent = plr.StarterGear
plr.leaderstats.Points.Value = plr.leaderstats.Points.Value - amount
game.Players.HasM9.Value = true
-- I have no problems with the Shop, it adds/denies purchases just fine.
3. I have a Data Store that saves values that looks like this:
game.Players.PlayerAdded:connect(function(player)
local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")
player:WaitForChild("leaderstats")
wait(1)
local stats = player:FindFirstChild("leaderstats"):GetChildren()
for i = 1, #stats do
stats[i].Value = datastore:GetAsync(stats[i].Name)
print("stat number "..i.." has been found")
stats[i].Changed:connect(function()
local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")
local statstorage = player:FindFirstChild("leaderstats"):GetChildren()
for i = 1, #statstorage do
datastore:SetAsync(statstorage[i].Name, statstorage[i].Value)
print("saved data number "..i)
end
print("Stats successfully saved")
end)
end
end)
Again, no problem saving the leaderstats values, so I tried adding the "HasM9" value...and I'm pretty sure how I went about it is all wrong. I have my items stored in ReplicatedStorage and a folder under ServerStorage called "ToolStorage".
4. My edited DataStore code:
game.Players.PlayerAdded:connect(function(player)
local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")
player:WaitForChild("leaderstats")
wait(1)
local stats = player:FindFirstChild("leaderstats"):GetChildren()
for i = 1, #stats do
stats[i].Value = datastore:GetAsync(stats[i].Name)
print("stat number "..i.." has been found")
-- Added "HasM9" lines below --
local HasM9 = player:GetAsync(player.userId)
or false
if HasM9 then
game.ServerStorage.ToolStorage.M9:Clone().Parent = player.Backpack
-- Added "HasM9" lines above --
stats[i].Changed:connect(function()
local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")
local statstorage = player:FindFirstChild("leaderstats"):GetChildren()
for i = 1, #statstorage do
datastore:SetAsync(statstorage[i].Name, statstorage[i].Value)
print("saved data number "..i)
end
print("Stats successfully saved")
end)
end
end -- extra End added on account of the M9 code.
end)
Am I remotely close to getting this right? I could really appreciate the help here. Thank you!

4 comments
just 1 tip, never ever ever ever ever ever ever ever ever use the Username of a player for datastore names and keys. if this person changes his username he will lose all data, use used id instead
also, why is the HasM9 inside of game.Players. it should be inside the actual player.
Good point, never thought of a player changing their username, but I still use their ID instead.
How ahould it be labeled then? I did put ot inaide the Players section, is that not how i call it?