I’m trying to save a tool using DataStores. For example, when a player leaves, a Boolean value gets saved. Then when the player joins, the Boolean value loads in and a tool clones itself based off the Boolean values name.
I have a folder for the tools, datastores, a toolMaker, and a toolhandler. My problem is that when a player loads in, the tool doesn’t appear in the players backpack.
I printed the problem out and it prints the players name and tools parent. Even so, it still doesn’t appear even after printing the correct output. I have 3 scripts that makes this work.
DataStoreHandler
local module = {}
function module:LoadData(player, folder, folderName, request)
local dataStore = DataStoreService:GetDataStore(folderName)
local playerUserId = player.UserId
local data
local success, errorMessage = pcall(function()
data = dataStore:GetAsync(folderName .."_".. playerUserId)
end)
if success and data and data ~= nil then
for i, v in data do
local value
if request == "ToolCreate" then
local newTool = toolFolder:FindFirstChild(i)
if newTool ~= nil then
giveItemServerSide:Invoke(player, newTool)
end
value = folder:FindFirstChild(i)
else
value = folder:FindFirstChild(i)
end
if value and not value:IsA("Folder") then
value.Value = v
end
end
else
print(errorMessage)
end
end
function module:SaveData(player, folder, folderName)
local dataStore = DataStoreService:GetDataStore(folderName)
local playerUserId = player.UserId
local data = {}
for i, v in folder:GetChildren() do
data[v.Name] = v.Value
end
local success, errorMessage = pcall(function()
dataStore:SetAsync(folderName .."_".. playerUserId, data)
end)
if success then
print("Data saved successfully!")
else
warn("Error saving data:", errorMessage)
end
end
return module
ToolMaker
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Folders
local remoteFolder = ReplicatedStorage:FindFirstChild("RemoteFolder")
local toolFolder = ReplicatedStorage:WaitForChild("ToolFolder")
-- Remotes
local giveItem = remoteFolder:FindFirstChild("GiveItem")
local giveItemServerSide = remoteFolder:FindFirstChild("GiveItemServerSide")
-- Variables
local toolInventory
-- Functions
local function makeTool(player, tool)
toolInventory = player:WaitForChild("ToolInventory")
local newAttribute = Instance.new("BoolValue")
newAttribute.Name = tool.Name
newAttribute.Parent = toolInventory
newAttribute.Value = true
local newTool = tool:Clone()
newTool.Parent = player.Backpack
print(newTool, newTool.Parent)
end
local function getTool(player, tool)
makeTool(player, tool)
end
-- Events
giveItem.OnServerInvoke = getTool
giveItemServerSide.OnInvoke = getTool
ToolHandler
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Folders
local moduleFolder = ReplicatedStorage:WaitForChild("ModuleFolder")
-- Modules
local dataStore = require(moduleFolder:WaitForChild("DataStore"))
-- Functions
local function createInventory(player)
local toolInventory = Instance.new("Folder")
toolInventory.Name = "ToolInventory"
toolInventory.Parent = player
dataStore:LoadData(player, toolInventory, toolInventory.Name, "ToolCreate")
end
local function saveInventory(player)
local toolInventory = player:FindFirstChild("ToolInventory")
dataStore:SaveData(player, toolInventory, toolInventory.Name)
end
-- Events
game.Players.PlayerAdded:Connect(createInventory)
game.Players.PlayerRemoving:Connect(saveInventory)

