Trying to clone a tool to the players backpack

Hello, I’ve been coding this basic system for plant growth. Im trying to make it so after the plant fully grows, you can pick it up as a tool. Everything works fine until the cloning the tool into the backpack, it doesn’t clone. I’ve looked through everywhere but can’t find the answer.

--plant growth script
--plant growth script
--plant growth script

local PLANT = script.Parent
local AREA = PLANT.Parent

local AREA_CD = AREA:WaitForChild("ClickDetector")

local CD = PLANT:WaitForChild("ClickDetector")

local TS = game:GetService("TweenService")
local RS = game:GetService("ReplicatedStorage")
local PLAYERS = game:GetService("Players")

local TOOL = RS.Plants.Test_Plants:WaitForChild("Test_Plant_Tool")


local SETTINGS = require(PLANT:WaitForChild("Settings"))

local STAGE_1_TIME = SETTINGS.STAGE_1_TIME
local STAGE_2_TIME = SETTINGS.STAGE_2_TIME
local STAGE_3_TIME = SETTINGS.STAGE_3_TIME

function plantStage1(size, color)
	
	local TWEEN_TIME = 1

	local GOAL = {}
	GOAL.Size = size
	GOAL.Color = color

	local TWEEN_INFO = TweenInfo.new(1, Enum.EasingStyle.Sine)

	local PlANT_TWEEN = TS:Create(PLANT, TWEEN_INFO, GOAL)

	if typeof(color) ~= "Color3" then
		error("Color Variable is not a BrickColor")
		return
	end

	if typeof(size) ~= "Vector3" then
		error("Size Variable is not a Vector3")
		return
	end
	
	PlANT_TWEEN:Play()
	
end

function plantStage2(size, color)

	local TWEEN_TIME = 1

	local GOAL = {}
	GOAL.Size = size
	GOAL.Color = color

	local TWEEN_INFO = TweenInfo.new(1, Enum.EasingStyle.Sine)

	local PlANT_TWEEN = TS:Create(PLANT, TWEEN_INFO, GOAL)

	if typeof(color) ~= "Color3" then
		error("Color Variable is not a BrickColor")
		return
	end

	if typeof(size) ~= "Vector3" then
		error("Size Variable is not a Vector3")
		return
	end

	PlANT_TWEEN:Play()
	
end

function plantStage3(size, color)
	
	local TWEEN_TIME = 1

	local GOAL = {}
	GOAL.Size = size
	GOAL.Color = color

	local TWEEN_INFO = TweenInfo.new(1, Enum.EasingStyle.Sine)

	local PlANT_TWEEN = TS:Create(PLANT, TWEEN_INFO, GOAL)

	if typeof(color) ~= "Color3" then
		error("Color Variable is not a BrickColor")
		return
	end

	if typeof(size) ~= "Vector3" then
		error("Size Variable is not a Vector3")
		return
	end

	PlANT_TWEEN:Play()
	
end

function plantGrowth()
	
	plantStage1(Vector3.new(2, 2, 2), Color3.new(0.807843, 0.360784, 1))
	
	task.wait(STAGE_1_TIME)
	
	plantStage2(Vector3.new(2, 3, 2), Color3.new(1, 0.419608, 0.0823529))
	
	task.wait(STAGE_2_TIME)
	
	plantStage3(Vector3.new(2, 5, 2), Color3.new(0.984314, 1, 0.117647))
	
	AREA_CD.MaxActivationDistance = 32
	
	CD.MaxActivationDistance = 32
	
end


function plantClicked(player)
	
	print("CLICEKD 1")
	

	game.Players.PlayerAdded:Connect(function(PLR)

		print("PLAYER ADDED")

		PLR.CharacterAdded:Connect(function()
			local BACKPACK = PLR:FindFirstChildOfClass("Backpack")
			
			if BACKPACK then
				local TOOL_CLONE = TOOL:Clone()
				print("CLICKED")
				TOOL_CLONE.Parent = BACKPACK
			end

		end)
	end)

	
	
end

plantGrowth()
CD.MouseClick:Connect(plantClicked)

Feel free to ask any questions.

1 Like

Just do PLR.Backpack and it will maybe work

1 Like

doesnt work. doesnt print “Player Added”. – forget to mention that in the original post

1 Like

Isn’t it what you are looking for?

function plantClicked(player)
    local BACKPACK = player:FindFirstChildWhichIsA("Backpack")

    if BACKPACK then
        TOOL:Clone().Parent = BACKPACK
    end
end

Seems like you are also trying to give the tool to all upcoming new players and give them back when they respawn, is it an expected behavior?

2 Likes

Mistakes

I absolutely agree with this, there is no need to add “RBXConnections” if the character or player is added, because it would ultimately just be ignored.

I would rather do this instead. If you still want these “Checks.”

-- code remains the same as before...

if PLR and PLR.Character then
   -- add tool to their character or backpack.
end

-- ...
2 Likes

This works. It is not an expected behavior though. But It doesn’t keep it after respawning so no need to worry.

1 Like

When the player dies, you can create a script to save their tools somewhere else and then put them back once their character is added, no?

2 Likes

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