Tool doesn't appear when player joins

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)

Are you giving the tool to the player locally or server-side?

Im giving the tool on serverside

Did you check whether the tool is a tool and if it is actually in the player backpack in the explorer? By what you said about the printing, it seems like the code is working.

Yes I did. It works when I set the parent to workspace. And I can grab it

But when the code executes can you find the tool with the explorer in Players → [your player name] → Backpack?

Edit: I know what I said above sounds obvious but your printing seems to suggest your code is working. Can you also let me know about what the tool’s parent is when printing inside the script.

The tools parent is player.backpack

Can you show the tool in explorer then inside the player’s backpack? I wonder if you are cloning the wrong thing because if it is a valid tool, then being in the player’s backpack is all that it needs for the tool to appear on the bottom of screen.

Screenshot 2024-09-15 100840

I’m confused is that from the script or manually placed? If its from the script, isn’t it working as intended?

I have a problem where it works when I buy it but when I rejoin, it doesn’t get added into the players backpack.

Does it print out the correct stuff for when the player rejoins? This may be an issue before the parenting and cloning of the tool into the backpack. Maybe a screenshot of the output and what you get in the end (a tool or not) could help me debug your datastore loading code.

From the printing, it seems like its not your datastore code. I wonder if its something to do with your player not being fully loaded when the items are parented. I’m sorry I keep asking for screenshots, but can you also check and take a screenshot of the player’s backpack in the explorer when you join the game?

I found the problem! I had to have the character load in for the tool to be parented to the backpack. I’m not really sure why that worked since character is in the workspace.

1 Like

That’s honestly a really pesky bug. I’m glad you found the issue!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.