Putting tool in player backpack not working

Hello Developers,

I am trying to create a part which turns a player into a robot and give them a laser gun when they touch the part. The morphing part works fine, but the laser gun-giving part doesn’t, and I do not know why because there are no errors. Please help if you can!

CODE:

local lasers = game:GetService("ReplicatedStorage"):WaitForChild("Lasers")

local morph = game.ServerStorage.Morphs.EvilRobot

local debounce = true
script.Parent.Touched:Connect(function(obj)
	local plr = game.Players:GetPlayerFromCharacter(obj.Parent)
	local backpack = plr:FindFirstChildOfClass("Backpack")
	if plr and debounce == true then
		debounce = false
		
		local morphClone = morph:Clone()
		morphClone.Name = plr.Name
		plr.Character = morphClone
		
		local rootPart = morphClone:FindFirstChild("HumanoidRootPart") or morphClone:FindFirstChild("Torso")
		local plrRoot = obj.Parent:FindFirstChild("HumanoidRootPart") or obj.Parent:FindFirstChild("Torso")
		
		if rootPart and plrRoot then
			rootPart.CFrame = plrRoot.CFrame
		end
		
		morphClone.Parent = workspace
		
		if backpack then
			local laserClone = lasers:Clone()
			laserClone.Parent = backpack
		end
		
		wait(.5)
		
		debounce = true
	end
	
end)
--this is the code which gives the player the tool

	if backpack then
			local laserClone = lasers:Clone()
			laserClone.Parent = backpack
		end
1 Like

What does the output say during the cloning process?

As I said, there are no errors in the output. The output is not displaying anything.

1 Like

Oh yeah sorry, didn’t notice that, does the morph have a Humanoid?

I think your issue is this line, print the backpack and see if it is nil. If so, just do ackpack = plr.Backpack inside of the if statement.

Yes, the morph has a humanoid.

It is not nil. I printed the value of backpack and it was “Backpack”.

Oh ok, so if there’s no error then I’m guessing the player’s character isn’t changing correctly. I’d probably just try cloning the morph into the StarterCharacter folder, and naming it “StarterCharacter” then calling player:LoadCharacter() and then destroying the morph afterwards.

The tool still isn’t being put in the player’s backpack.

(I put the code which is supposed to plant the tool in the player’s backpack AFTER the :LoadCharacter() function.)

Ok, I’m not sure what the problem may be, but I recreated script with what I believe your assets look like, and it works nicely:

local part = script.Parent
local morph = game:GetService("ServerStorage"):WaitForChild("Morphs").EvilRobot
local lasers = game:GetService("ReplicatedStorage"):WaitForChild("Lasers")
local debounce = false
part.Touched:Connect(function(obj)
	if obj ~= nil and obj.Parent:FindFirstChild("Humanoid") ~= nil and game:GetService("Players"):FindFirstChild(obj.Parent.Name) ~= nil and debounce == false then debounce = true
		local plr = game:GetService("Players"):GetPlayerFromCharacter(obj.Parent)
		local morphClone = morph:Clone()
		morphClone.Name = "StarterCharacter"
		morphClone.Parent = game:GetService("StarterPlayer")
		local lastCharacterPos = plr.Character:WaitForChild("HumanoidRootPart").Position
		plr:LoadCharacter()
		morphClone:Destroy()
		local lasersClone = lasers:Clone()
		lasersClone.Parent = plr:WaitForChild("Backpack")
		plr.Character:MoveTo(lastCharacterPos + Vector3.new(0, 0, -5))
		debounce = false
	end
end)

Thank you so much! This worked like a charm.

1 Like

No problem, thanks for the update.

1 Like

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