Hello guys, I have an issue with my item data store where if you hold an item and then leave with it, it does not save. But if you don’t hold an item, It will save.
Here’s the code:
local dss = game:GetService("DataStoreService")
local toolds = dss:GetDataStore("ToolDS")
local toolcontain = game.ServerStorage.Items
game.Players.PlayerRemoving:Connect(function(player)
wait(2)
local playerbp = {}
for i, v in ipairs(player.Backpack:GetChildren()) do
if v:IsA('Tool') then
table.insert(playerbp, v.Name)
end
end
toolds:SetAsync(player.UserId, playerbp)
end)
game.Players.PlayerAdded:Connect(function(player)
local playerbp = toolds:GetAsync(player.UserId) or {}
for i, v in ipairs(playerbp) do
if toolcontain:FindFirstChild(v) then
toolcontain[v]:Clone().Parent = player.Backpack
toolcontain[v]:Clone().Parent = player.StarterGear
end
end
end)
game:BindToClose(function()
wait(3)
end)
local plrCharacterTools = {}
game.Players.PlayerAdded:Connect(function(player)
player.CharacterRemoving:Connect(function(char)
plrCharacterTools[plr.UserId] = char:FindFirstChildOfClass("Tool")
end)
local playerbp = toolds:GetAsync(player.UserId) or {}
for i, v in ipairs(playerbp) do
if toolcontain:FindFirstChild(v) then
toolcontain[v]:Clone().Parent = player.Backpack
toolcontain[v]:Clone().Parent = player.StarterGear
end
end
end)
and then in your ‘PlayerRemoving’ you can access the plrCharacterTools dictionary
Have you tried this without being in Roblox studio? Roblox Studio doesn’t handle BindToClose very well. You can work around this if this is the case. Make sure you are in Roblox Studio and then do a time loop to save
local dss = game:GetService("DataStoreService")
local toolds = dss:GetDataStore("ToolDS")
local toolcontain = game.ServerStorage.Items
game.Players.PlayerRemoving:Connect(function(player)
wait(2)
local playerbp = {}
for i, v in ipairs(player.Backpack:GetChildren()) do
if v:IsA('Tool') then
table.insert(playerbp, v.Name)
end
end
if player.Character:FindFirstChildWhichIsA("Tool") then
table.insert(playerbp,player.Character:FindFirstChildWhichIsA("Tool").Name)
end
toolds:SetAsync(player.UserId, playerbp)
end)
game.Players.PlayerAdded:Connect(function(player)
local playerbp = toolds:GetAsync(player.UserId) or {}
for i, v in ipairs(playerbp) do
if toolcontain:FindFirstChild(v) then
toolcontain[v]:Clone().Parent = player.Backpack
toolcontain[v]:Clone().Parent = player.StarterGear
end
end
end)
game:BindToClose(function()
wait(3)
end)
Hey I just found out that CharacterRemoving fires after PlayerRemoving, to counter this, we can write code like this:
local Players = game:GetService("Players")
local playerRemovingBPDatas = {}
Players.PlayerAdded:Connect(function(plr)
plr.CharacterRemoving:Connect(function(char)
print("char removing")
if playerRemovingBPDatas[tostring(plr.UserId)] then
local tool = char:FindFirstChildOfClass("Tool")
if tool then
table.insert(playerRemovingBPDatas[tostring(plr.UserId)], tool.Name)
end
print(playerRemovingBPDatas[tostring(plr.UserId)]) -- print all tools for proof
-- DO YOUR DATA SAVING HERE
-- after saving data, you can also do "playerRemovingBPDatas[tostring(plr.UserId)] = nil" to clear that user backpack data in the dictionary to save game memory
end
end)
end)
Players.PlayerRemoving:Connect(function(plr)
local plrBP = {}
for _,tool in pairs(plr.Backpack:GetChildren()) do
if tool:IsA("Tool") then
table.insert(plrBP, tool.Name)
end
end
playerRemovingBPDatas[tostring(plr.UserId)] = plrBP
end)
The reason it didn’t get the character is because you had an unnecessary wait
local dss = game:GetService("DataStoreService")
local toolds = dss:GetDataStore("ToolDS")
local toolcontain = game.ServerStorage.Items
game.Players.PlayerRemoving:Connect(function(player)
local playerbp = {}
for i, v in ipairs(player.Backpack:GetChildren()) do
if v:IsA('Tool') then
table.insert(playerbp, v.Name)
end
end
if player.Character:FindFirstChildWhichIsA("Tool") then
table.insert(playerbp,player.Character:FindFirstChildWhichIsA("Tool").Name)
end
toolds:SetAsync(player.UserId, playerbp)
end)
game.Players.PlayerAdded:Connect(function(player)
local playerbp = toolds:GetAsync(player.UserId) or {}
for i, v in ipairs(playerbp) do
if toolcontain:FindFirstChild(v) then
toolcontain[v]:Clone().Parent = player.Backpack
toolcontain[v]:Clone().Parent = player.StarterGear
end
end
end)
game:BindToClose(function()
task.wait(3)
end)