Current Situation:
Right now, I have a data-saving system that stores tools by saving the names. When the player rejoins, those tools are simply restored by inserting them back into their inventory from a folder in ServerStorage. This works well for predefined tools.
The Problem:
Some tools in my game are generated dynamically during gameplay. For example, a fish the player catches from a pond. These fish tools have random properties like weight or size that are set when they’re created. Since these are not predefined tools and their attributes vary, I don’t know how to save and restore them properly when the player rejoins.
What I Need:
I’m looking for a way to:
- Save dynamically generated tools (like a fish with custom weight) and their attributes.
- Restore them accurately when the player rejoins, with the same properties they had before.
Here is the ServerScript I am currently using:
local ds = game:GetService(“DataStoreService”):GetDataStore(“ToolSave”)
game.Players.PlayerAdded:connect(function(plr)
local key = “id-”…plr.userId
pcall(function()
local tools = ds:GetAsync(key)
if tools then
for i,v in pairs(tools) do
local tool = game.ServerStorage.Tools:FindFirstChild(v)
if tool then
tool:Clone().Parent = plr:WaitForChild(“Backpack”)
tool:Clone().Parent = plr:WaitForChild(“StarterGear”)
end
end
end
end)
end)
game.Players.PlayerRemoving:connect(function(plr)
local key = “id-”…plr.userId
pcall(function()
local toolsToSave = {}
for i,v in pairs(plr.Backpack:GetChildren()) do
if v then
table.insert(toolsToSave,v.Name)
end
end
ds:SetAsync(key,toolsToSave)
end)
end)