Hi!
I can't get my DataStore script to save the Tools purchased. I don't know why. I don't get any error for it. The leaderstats save, the Tools do not. I've included my entire Leaderboard/DataStore script.
I've included two versions I've been advised to try for the DataStore, the first one tries to save it into a Folder, the second one checks for BoolValues, but neither one will save the Tools, just the leaderstats.
First attempt with a Folder:
game.Players.ChildAdded:connect(function(newPlayer)
local stats = Instance.new('IntValue')
stats.Name = 'leaderstats'
local points = Instance.new('IntValue', stats)
points.Name = 'Points'
points.Value = 0
local experience = Instance.new('IntValue', stats)
experience.Name = 'EXP'
experience.Value = 0
local wins = Instance.new('IntValue', stats)
wins.Name = 'Wins'
wins.Value = 0
local kills = Instance.new('IntValue', stats)
kills.Name = 'Kills'
kills.Value = 0
local level = Instance.new('IntValue', stats)
level.Name = 'Level'
level.Value = 1
-- MAX HEALTH INCREASE
if newPlayer.Character and newPlayer.Character:FindFirstChild("Humanoid") then
newPlayer.Character.Humanoid.MaxHealth = 100 + level.Value*10
end
level.Changed:connect(function(value)
if newPlayer.Character and newPlayer.Character:FindFirstChild("Humanoid") then
newPlayer.Character.Humanoid.MaxHealth = 100 + level.Value*10
end
end)
newPlayer.CharacterAdded:connect(function(character)
character:WaitForChild("Humanoid").MaxHealth = 100 + level.Value*10
end)
-- EXP NEEDED TO LEVEL UP
experience.Changed:connect(function()
local required=(level.Value*70) -- Number reflects the EXP points needed to level up.
if experience.Value >= required then
level.Value=level.Value+1
experience.Value=experience.Value-required
end
end)
--DATASTORE
stats.Parent = newPlayer
local datastore = game:GetService("DataStoreService"):GetDataStore(newPlayer.Name.."Stats")
local tools = game.ServerStorage.ToolStorage;-- ADDED FOR TOOLS
local stats = stats:GetChildren()
for i = 1, #stats do
stats[i].Value = datastore:GetAsync(stats[i].Name)
print("stat number "..i.." has been found")
end
local save_interval = 60 * 2 -- every 2 minutes
wait(save_interval)
while newPlayer.Parent do
local stats = newPlayer.leaderstats:GetChildren()
for i = 1, #stats do
datastore:SetAsync(stats[i].Name, stats[i].Value)
print("stat number "..i.." has been auto-saved")
end
local data = false
print("DataStoreBackSave1")
newPlayer.CharacterAdded:connect(function(char) -- this will run everytime the Player's Character has been added in workspace.
if not data then
data = datastore:GetAsync(newPlayer.UserId);
end
if data then
--If it does, loop and clone respective tools.
for _,v in next,data do
local tool = tools:FindFirstChild(v);
if tool then
tool:Clone().Parent = newPlayer.Backpack;
end
end
end
end)
end
wait(save_interval)
end)
game.Players.ChildRemoved:connect(function(oldPlayer)
local datastore = game:GetService("DataStoreService"):GetDataStore(oldPlayer.Name.."Stats")
local stats = oldPlayer.leaderstats:GetChildren()
for i = 1, #stats do
datastore:SetAsync(stats[i].Name, stats[i].Value)
print("stat number "..i.." has been saved")
--Make a table to hold all the data
local toolList = {};
--Iterate through the backpack and fill the table in
for _,v in next,oldPlayer.Backpack:GetChildren() do
--Make sure it's actually a tool
if v:IsA("Tool") or v:IsA("HopperBin") then
toolList[#toolList+1] = v.Name;
end
end
--Save the table
datastore:SetAsync(oldPlayer.UserId,toolList);
end
print("DataStoreBackSave2")
end)
Second attempt with BoolValues:
stats.Parent = newPlayer
local datastore = game:GetService("DataStoreService"):GetDataStore(newPlayer.Name.."Stats")
local stats = stats:GetChildren()
for i = 1, #stats do
stats[i].Value = datastore:GetAsync(stats[i].Name)
print("stat number "..i.." has been found")
end
local save_interval = 60 * 2 -- every 2 minutes
wait(save_interval)
while newPlayer.Parent do
local stats = newPlayer.leaderstats:GetChildren()
for i = 1, #stats do
datastore:SetAsync(stats[i].Name, stats[i].Value)
print("stat number "..i.." has been auto-saved")
end
wait(save_interval)
end
newPlayer.PlayerGui:WaitForChild("HasM9").Changed:Connect(function()
if newPlayer.PlayerGui.HasM9.Value == true then
local DataStorem9 = game:GetService("DataStoreService"):GetDataStore("HasM9")
game.ServerStorage.ToolStorage.M9:Clone().Parent = newPlayer.Backpack
DataStorem9:SetAsync(newPlayer.userId, true)
end
end)
end)
game.Players.ChildRemoved:connect(function(oldPlayer)
local datastore = game:GetService("DataStoreService"):GetDataStore(oldPlayer.Name.."Stats")
local stats = oldPlayer.leaderstats:GetChildren()
for i = 1, #stats do
datastore:SetAsync(stats[i].Name, stats[i].Value)
print("stat number "..i.." has been saved")
end
oldPlayer.PlayerGui:WaitForChild("HasM9").Changed:Connect(function()
if oldPlayer.PlayerGui.HasM9.Value == true then
local DataStorem9 = game:GetService("DataStoreService"):GetDataStore("HasM9")
DataStorem9:SetAsync(oldPlayer.userId, true)
end
end)
end)
I could really appreciate some help here, this has been bothering me for months now. x.x

10 comments
You should be able to.
I agree, but it doesn't. When the player dies, or leaves the game and returns, the item is no longer in their backpack.
would you like to see my data store
Sure! Would be happy to see one that saves the tools! :) Sent a friend request.
will do tommorow as on my phone of now
^ Many thanks good sir! :)
https://uploadfiles.io/ <<read your roblox messages for instructions
Ah! Saw this late, sent the message, uploaded the file - https://ufile.io/lswdu
https://drive.google.com/file/d/0BzASRbzuZMuGWE9ZckNyRjg5UXM/edit
I uploaded on google drive because it was easier