Hello, i have Data Store that saves tools even u leave and join back, i noticed it only saves if u pick it up, how would i make it save if it gets cloned to your backpack?
local dss = game:GetService("DataStoreService")
local toolsDS = dss:GetDataStore("ToolsData1")
local toolsFolder = game.ServerStorage:WaitForChild("ToolsFolder1")
game.Players.PlayerAdded:Connect(function(plr)
local toolsSaved = toolsDS:GetAsync(plr.UserId .. "-tools") or {}
for i, toolSaved in pairs(toolsSaved) do
if toolsFolder:FindFirstChild(toolSaved) then
toolsFolder[toolSaved]:Clone().Parent = plr.Backpack
toolsFolder[toolSaved]:Clone().Parent = plr.StarterGear
end
end
plr.CharacterRemoving:Connect(function(char)
char.Humanoid:UnequipTools()
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
local toolsOwned = {}
for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do
table.insert(toolsOwned, toolInBackpack.Name)
end
local success, errormsg = pcall(function()
toolsDS:SetAsync(plr.UserId .. "-tools", toolsOwned)
end)
if errormsg then warn(errormsg) end
end)
game:BindToClose(function(plr) -- Runs whenver the server is about to shut down/stop.
local toolsOwned = {}
for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do
table.insert(toolsOwned, toolInBackpack.Name)
end
local success, errormsg = pcall(function()
toolsDS:SetAsync(plr.UserId .. "-tools", toolsOwned)
end)
if errormsg then warn(errormsg) end
end)
Yeah sure, basically I have a shop system and when u purchase a tool it gets cloned to your backpack and I tried using this data store but it only saves the tool when u pick it up, it doesn’t save it if it gets cloned so how would I save it if it gets cloned u know what u mean? Tell me if I explained it bad lol
game.Players.PlayerRemoving:Connect(function(plr)
plr:WaitForChild("Character"):WaitForChild("Humanoid"):UnequipTools()
local toolsOwned = {}
for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do
table.insert(toolsOwned, toolInBackpack.Name)
end
local success, errormsg = pcall(function()
toolsDS:SetAsync(plr.UserId .. "-tools", toolsOwned)
end)
if errormsg then warn(errormsg) end
end)
so you unequip the tool if the player equips it
It will automatically put the tool inside the backpack
Remember DataStores are server-sided and when you move it to your own backpack, you’re on client-side, so the server thinks there’s nothing on your backpack. Everything with the code is correct, you just need to switch to server-side while changing the tool’s parent with this icon:
Also, if you are planning on creating a UI shop, don’t save data with a LocalScript, instead use a RemoteEvent to handle saving on the server.
For a last a recommendation, to not repeat code, create a function and call it everytime you save data.
local function SaveData(plr)
local toolsOwned = {}
for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do
table.insert(toolsOwned, toolInBackpack.Name)
end
local success, errormsg = pcall(function()
toolsDS:SetAsync(plr.UserId .. "-tools", toolsOwned)
end)
if errormsg then warn(errormsg) end
end
Players.PlayerRemoving:Connect(SaveData)
game:BindToClose(function()
for _, plr in ipairs(Players:GetPlayers()) do
SaveData(plr)
end
end)