Cloned tool not being parented to backpack

Hello, I’m making a simulator game involving paper airplanes,
Once I throw the airplane and it touches the ground it should wait 2 seconds and then be destroyed, then have a clone parent it to backpack. (Sorry if this is an inefficient way, i’m new)
this is my current code:

workspace.DescendantRemoving:Connect(function(part)
    if part.Name == "PaperAirplane" then
        print("parented")
        ClonedTool.Parent = player.Backpack
    end
end)

There is no error, and it gets destroyed as it should. Nothing prints either.
I’ve also tried this code:

script.Parent.Handle.Touched:Connect(function(hit)

	print("hit")

	if hit.Parent:FindFirstChild("Part") then
		print("Is part")
		wait(2)
		script.Parent:Destroy()

		local ClonedTool = game.ReplicatedStorage.PaperAirplane:Clone()

		ClonedTool.Parent = game.Players.LocalPlayer.Backpack

		print("Destroyed")
	end
end)

But it gives me this error: attempt to index nil with ‘Backpack’

Any help is appreciated, thanks.

1 Like

game.Players.LocalPlayer doesnt exist in server scripts you should do it like this:

local isTouched=false

script.Parent.Handle.Touched:Connect(function(hit)
	print("hit")
	if isTouched then
		return
	end
	if game.Players:FindFirstChild(hit.Parent.Name) then
		isTouched = true
		local player = game.Players:WaitForChild(hit.Parent.Name)
		local ToolName = "PaperAirplane"
		local source = game.ServerStorage
		
		if source:FindFirstChild(ToolName) then
			local backpack = player.Backpack
			if backpack then
				source[ToolName]:Clone().Parent = backpack
			end
		end
	end
	task.wait(1.25)
	script.Parent:Destroy()
end)

Hello, Thanks for the help but it doesnt parent to the players backpack, it does get destroyed, yes.

do you want to clone it to all players backpack? because youre using Players.LocalPlayer

No, just the person that threw the airplane.

local __CS = game:GetService("CollectionService")
script.Parent.Touched:Connect(function(__PART:BasePart?)
	local __PLAYER = game:GetService("Players"):GetPlayerFromCharacter(__PART.Parent)
	if __PLAYER ~= nil then
		if not __CS:HasTag(__PLAYER, "__CD") then
			task.delay(1,function()
				__CS:RemoveTag(__PLAYER)
			end)
			local __TOOL = game:GetService("ServerStorage"):FindFirstChild("TOOLNAMEHRE"):Clone()
			__TOOL.Parent = __PLAYER.Backpack
			script.Parent:Destroy()
		end
	end
end)

like this?

you need to define the player which is airplane threw, and then clone it to the player which is you defined.

Try this when you get the chance

local Handle = script.Parent.Handle

Handle.Touched:Connect(function(hit)
	
	local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	
	if (Player) then
		
		script.Parent:Destroy()

		local ClonedTool = game.ReplicatedStorage.PaperAirplane:Clone()

		ClonedTool.Parent = Player.Backpack

		print("Destroyed")
		
	end
	
end)

Any errors, let me know!

Hi. I want the airplane to be destroyed after it touches a part. Preferably after 2 seconds, then it creates a clone and parents it to the players backpack.

local Handle = script.Parent.Handle

Handle.Touched:Connect(function(hit)
	
	local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	
	if (Player) then

		wait(2)

		script.Parent:Destroy()

		local ClonedTool = game.ReplicatedStorage.PaperAirplane:Clone()

		ClonedTool.Parent = Player.Backpack

		print("Destroyed")
		
	end
	
end)

??

Sorry but the paper airplane doesnt get destroyed when it touches a part

Thats not how that works. You’d need a script inside the airplane when the Airplane collides with a part. Or do this

local Handle = script.Parent.Handle

Handle.Touched:Connect(function(hit)
	
	local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	
	if (Player) then

		wait(2)

		script.Parent:Destroy()

		local ClonedTool = game.ReplicatedStorage.PaperAirplane:Clone()

		ClonedTool.Parent = Player.Backpack

                ClonedTool.InsertPartToCollide.Touched:Connect(function(hit)
                           PartToDestroy:Destroy()
                end)

		print("Destroyed")
		
	end
	
end)

Is it a Server-Side script or LocalScript ?

hello, It is a server side script

I dont understand, sorry. I want it to detect when it touches a part and then destroys itself, then puts the clone into the players backpack. And rinse and repeat

So you don’t need to use game.Players.LocalPlayer. It’s used in LocalScript. Also, can you send the whole code, so that we could check where you defined a player ?

If you destroy a part and then try to clone, it won’t, because they are opposite and you can’t really clone something that doesn’t exist.

local tool = script.Parent
local Player = game:GetService("Players")
Player:WaitForChild("Backpack", 5)
local ClonedTool = game.ReplicatedStorage.PaperAirplane:Clone()
tool.Bounce.OnServerEvent:Connect(function(player, event)
	print(player)
local mouse = player:GetMouse()
	local ball = tool.Handle
	tool.Parent = workspace
	ball.CFrame = tool.Handle.CFrame
		ball.CFrame = CFrame.new(ball.Position, event)
		
		local BV = Instance.new("BodyVelocity", ball);
		BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		BV.Velocity = ball.CFrame.lookVector * 50 -- how fast it throws
		wait(.1)
		tool.Handle.BodyVelocity:Destroy()
end)

local isTouched=false

local Handle = script.Parent.Handle

script.Parent.Handle.Touched:Connect(function(hit)

	print("hit")

	if hit.Parent:FindFirstChild("Part") then
		print("Is part")
		wait(2)
		script.Parent:Destroy()

		local ClonedTool = game.ReplicatedStorage.PaperAirplane:Clone()

		ClonedTool.Parent = game.Players.LocalPlayer.Backpack

		print("Destroyed")
	end
end)

Theres one paper airplane in the starterpack and replicated storage.

I found the error.

local Player = game:GetService("Players")
Player:WaitForChild("Backpack", 5)

Here why you used Player:WaitForChild(“Backpack”, 5) ?
You aren’t defining a backpack.

And you are actually waiting for backpack for a service, not actual player.

To define indivisual player, use a local script and use game.Players.LocalPlayer

Or use game.Players.PlayerAdded:Connect(function(player)
–code
)

And why you used LocalPlayer when it isn’t a LocalScript ?