I was about to post my comment, but then I realized what might be happening.
If the clonedTool is being parented to the Backpack before the player’s Character first spawns, it might be being cleared once the Character appears, which is why the original Tools are appearing as null. I’d think that it would just show an empty Backpack in that case, but that might be something to consider for another revision.
In the meantime, the changes you made for that codeblock should be good to test for now. While you test it, I’ll slightly revise it and post a follow-up reply to include a check for the first time the player spawns to see if that could resolve it
Here’s an example revision to wait to clone the Tools until the player’s Character spawns for the first time after joining:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HTTP = game:GetService("HttpService")
local function onPlayerJoin(player)
local Backpack = player:WaitForChild("Backpack")
local joinData = player:GetJoinData()
local teleportData = joinData.TeleportData
print("joinData:", joinData, "JsonEncodedData:", HTTP:JSONEncode(joinData))
print("teleportData:", teleportData, "JsonEncodedData:", HTTP:JSONEncode(teleportData))
print("teleportData.equippedTools", teleportData.equippedTools)
local firstSpawn
local function onCharacterSpawn()
if firstSpawn == true then return end
firstSpawn = true
if teleportData and teleportData.equippedTools then
local equippedTools = teleportData.equippedTools
local toolNameTable = equippedTools[tostring(player.UserId)]
print("toolNameTable:", toolNameTable, "JsonEncodedData:", HTTP:JSONEncode(toolNameTable))
if toolNameTable then
for _, toolName in toolNameTable do
print("toolName",toolName)
local realTool = ReplicatedStorage:FindFirstChild(toolName)
if realTool then
print("RealTool",realTool)
local clonedTool = realTool:Clone()
print("Cloned_Tool:", clonedTool)
print("ClonedToolType", typeof(clonedTool), "ClonedTool:Children", HTTP:JSONEncode(clonedTool:GetChildren()))
task.defer(function()
clonedTool.Parent = Backpack
end)
print("Players's Backpack:", HTTP:JSONEncode(player.Backpack:GetChildren()))
print("Cloned.Parent:", clonedTool.Parent)
end
end
else
warn("No tool names found for player with UserId: " .. player.UserId)
end
else
warn("No TeleportData or equippedTools found for player with UserId: " .. player.UserId)
end
end
if player.Character then
onCharacterSpawn()
end
player.CharacterAdded:Once(onCharacterSpawn)
end
for _, player in Players:GetPlayers() do
task.spawn(onPlayerJoin, player)
end
Players.PlayerAdded:Connect(onPlayerJoin)
Just to clarify, was that the Output from the original test or was it with the revised code that waits for the Character to spawn in for the first time (from my last reply)?
Just want to be sure since you replied to my post with the revised code
So I went into Roblox Studio and tested a similar setup (with only minimal code within onCharacterAdded for cloning a Tool into the backpack) and the same thing happened.
But once again, I made one simple change… and in this case, I have no idea why it worked. I moved the reference to the Backpack (local Backpack = player:WaitForChild("Backpack")) into the onCharacterSpawn function and the Tool was successfully parented there without disappearing. It’s almost as if the initial reference to the Backpack is not the current Backpack of the player by the time it clones the Tools.
Hopefully that will be the final change that is necessary for it to work