Hi! I’m using a Tool Datastore but I can’t get it to save tools that are equip (aka in the player’s character). I’ve tried referencing the character when leaving but this doesn’t work so I’m not sure what other method I should use.
Here is the script:
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("BackpackSave")
game.Players.PlayerAdded:Connect(function(player)
local success, err = pcall(function()
local tool = dataStore:GetAsync("User-"..player.UserId)
if tool then
print("Got Async!")
for i,v in pairs(tool) do
local toolFound = game.ReplicatedStorage.Items:FindFirstChild(v)
if toolFound then
toolFound:Clone().Parent = player.Backpack
toolFound:Clone().Parent = player.StarterGear
print("Data Loaded!")
end
end
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
print("Connected!")
local playerleft = false
local toolsSave = {}
for i, tool in pairs(player.Backpack:GetChildren()) do
print("Got Backpack!")
if tool then
table.insert(toolsSave,tool.Name)
print("Put into Table!")
end
print("Data Checked!")
end
dataStore:SetAsync("User-"..player.UserId, toolsSave)
print("Data Saved!")
game:BindToClose(function()
dataStore:SetAsync("User-"..player.UserId, toolsSave)
end)
end)
Your only saving Data when the Server is closing or shutting down.
game:BindToClose(function()--Fires When the games shuts down, not when player leaves
dataStore:SetAsync("User-"..player.UserId, toolsSave)
print("Data Saved!")
end)
I completely forgot to add a second one without the binding, thanks for pointing that out! Saving and cross-saving between places is now fixed. But I’m still struggling to make guns that the player is holding to save. I used to parent the tool that was inside the character to the backpack but I’ve found that you can no longer reference the character when the player is leaving, so I’m not sure how to do it.
I suggest you keep track of all tools inside the player before they leave the game, in a table.
Use something like ChildAdded/Removed on the backback to keep track of tools (make sure it only removes a tool from the table if it does not exist in both the player and backpack) then save that table when they leave the game.
In the script it seems like you’re collecting the tools when the player leaves, you should always have the tools collected in a table, even with the player still in game, personally the best way to do it since you can always access that table even when players leave.
I’ve tried to do that, but I can’t seem to get it to work, but it’s like 1am I’ve probably missed out something small or done something stupid so it’s probably best if a second pair of eyes checks my code. I’m not getting any errors, apart from that the only print I get is “Got Async” and the fact that it completely breaks.
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("BackpackSave")
local globalplr
local toolsSave = {}
game.Players.PlayerAdded:Connect(function(player)
local success, err = pcall(function()
globalplr = player
local tool = dataStore:GetAsync("User-"..player.UserId)
if tool then
print("Got Async!")
for i,v in pairs(tool) do
local toolFound = game.ReplicatedStorage.Items:FindFirstChild(v)
if toolFound then
toolFound:Clone().Parent = player.Backpack
toolFound:Clone().Parent = player.StarterGear
print("Data Loaded!")
end
end
end
end)
end)
local char = globalplr.Character
char.ChildAdded:Connect(function()
for i, tool in pairs(globalplr.Backpack:GetChildren()) do
print("Got Backpack!")
for i, tool in pairs(globalplr.Character:GetChildren()) do
print("Got Character!")
if tool then
table.insert(toolsSave,tool.Name)
print("Put into Table!")
end
end
end
end)
char.ChildRemoved:Connect(function()
for i, tool in pairs(globalplr.Backpack:GetChildren()) do
print("Got Backpack!")
if tool then
table.insert(toolsSave,tool.Name)
print("Put into Table!")
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
print("Connected!")
dataStore:SetAsync("User-"..player.UserId, toolsSave)
print("Data Saved!")
game:BindToClose(function()
dataStore:SetAsync("User-"..player.UserId, toolsSave)
end)
end)
for i,v in pairs(tool) do
local toolFound = game.ReplicatedStorage.Items:FindFirstChild(v)
print(toolFound)
if toolFound then
toolFound:Clone().Parent = player.Backpack
toolFound:Clone().Parent = player.StarterGear
print("Data Loaded!")
end
end
Perhaps try doing this. Cause could be also that tool is empty, thus the loop is never running.
This for me, works way better than table.insert or table.remove, table.insert/remove has caused a lot of issues for me that only got fixed after I started adding/removing values like this.
Besides, you used table.insert/remove wrong.
They can only use numbers, table.insert(toolsSave,tool.Name) is not going to work.
Should be table.insert(toolsSave, tool.Name, #toolsSave + 1) instead.