I have a script where it says the players backpack items, but it doesn’t saves the player’s held item, how would I do this? here’s my current script:
local DataStore = game:GetService("DataStoreService")
local CurrentData = DataStore:GetDataStore("Items")
game.Players.PlayerAdded:Connect(function(Player)
local Data
local success, whoops = pcall(function()
Data = CurrentData:GetAsync(Player.UserId)
end)
if success and Data then
for _, Tool in pairs(Data) do
local ToolCheck = game.ReplicatedStorage.Items:FindFirstChild(Tool)
if ToolCheck then
local ToolClone = ToolCheck:Clone()
ToolClone.Parent = Player.Backpack
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local ToolsToSave = {}
for _, Tool in pairs(Player.Backpack:GetChildren()) do
table.insert(ToolsToSave, Tool.Name)
end
local success, whoops = pcall(function()
CurrentData:SetAsync(Player.UserId, ToolsToSave)
end)
if success then
print("Data saved")
else
warn(whoops)
end
end)
your code isn’t messing any thing just try to add bind Close when the game close this maybe it
local DataStore = game:GetService("DataStoreService")
local CurrentData = DataStore:GetDataStore("Items")
game.Players.PlayerAdded:Connect(function(Player)
local Data
local success, whoops = pcall(function()
Data = CurrentData:GetAsync(Player.UserId)
end)
if success then
Data = Data or {}
for _, Tool in pairs(Data) do
local ToolCheck = game.ReplicatedStorage.Items:FindFirstChild(Tool)
if ToolCheck then
local ToolClone = ToolCheck:Clone()
ToolClone.Parent = Player.Backpack
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local ToolsToSave = {}
for _, Tool in pairs(Player.Backpack:GetChildren()) do
table.insert(ToolsToSave, Tool.Name)
end
local success, whoops = pcall(function()
CurrentData:SetAsync(Player.UserId, ToolsToSave)
end)
if success then
print("Data saved")
else
warn(whoops)
end
end)
game:BindToClose(function()
for _ , player in game.Players:GetPlayers() do
task.spawn(function()
local ToolsToSave = {}
for _, Tool in pairs(Player.Backpack:GetChildren()) do
table.insert(ToolsToSave, Tool.Name)
end
local success, whoops = pcall(function()
CurrentData:SetAsync(Player.UserId, ToolsToSave)
end)
if success then
print("Data saved")
else
warn(whoops)
end
end)
end
end)
local DataStore = game:GetService("DataStoreService")
local CurrentData = DataStore:GetDataStore("Items")
game.Players.PlayerAdded:Connect(function(Player)
local Character = Player.Character
local Tool = Character:FindFirstChildWhichIsA("Tool")
if Tool then
local success, whoops = pcall(function()
CurrentData:SetAsync(Player.UserId, Tool.Name)
end)
if success then
print("Data saved")
else
warn(whoops)
end
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local ToolsToSave = {}
for _, Tool in pairs(Player.Backpack:GetChildren()) do
if Tool:IsA("Tool") then
table.insert(ToolsToSave, Tool.Name)
end
end
local success, whoops = pcall(function()
CurrentData:SetAsync(Player.UserId, ToolsToSave)
end)
if success then
print("Data saved")
else
warn(whoops)
end
end)
game.Players.PlayerAdded:Connect(function(Player)
local success, data = pcall(function()
return CurrentData:GetAsync(Player.UserId)
end)
if success then
if data then
for _, Tool in pairs(data) do
local ToolClone = game.ReplicatedStorage.Items:FindFirstChild(Tool):Clone()
ToolClone.Parent = Player.Backpack
end
end
else
warn(whoops)
end
end)
I’m back from school, here’s the current script I have:
local DataStore = game:GetService("DataStoreService")
local CurrentData = DataStore:GetDataStore("Items")
game.Players.PlayerAdded:Connect(function(Player)
local Data
local success, whoops = pcall(function()
Data = CurrentData:GetAsync(Player.UserId)
end)
if success and Data then
for _, Tool in pairs(Data) do
local ToolCheck = game.ReplicatedStorage.Items:FindFirstChild(Tool)
if ToolCheck then
local ToolClone = ToolCheck:Clone()
ToolClone.Parent = Player.Backpack
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local ToolsToSave = {}
Player.CharacterRemoving:Connect(function(char)
char:WaitForChild("Humanoid"):UnequipTools()
end)
for _, Tool in pairs(Player.Backpack:GetChildren()) do
table.insert(ToolsToSave, Tool.Name)
end
local success, whoops = pcall(function()
CurrentData:SetAsync(Player.UserId, ToolsToSave)
end)
if success then
print("Data saved")
else
warn(whoops)
end
end)