Why wont my tool move to backpack?

I have made it so when the humanoid dies, they drop a tool, but the tool wont move to the players backpack when i activate the prompt.
it just says " Workspace.NPC EnemySoldier.Script:13: attempt to index nil with ‘Backpack’" in errors

this is what I have in a Script
(the code is a bit messy)

local replicatedstorage = game:WaitForChild("ReplicatedStorage")
local gun = replicatedstorage.Guns:WaitForChild("AK47")
local player = game.Players.LocalPlayer

script.Parent.Humanoid.Died:Connect(function()
	print("dead")
	local clone = gun:Clone()
	clone.Parent = game.Workspace
	local l = script.Parent.Torso.Position
	clone:MoveTo(Vector3.new(l.X,l.Y,l.Z))
	clone.Handle:WaitForChild("TouchInterest"):Destroy()
	clone.Handle.ProximityPrompt.Triggered:Connect(function()
		clone.Parent = player.Backpack
		script.Parent:Destroy()
	end)
end)

I don’t see what’s wrong

The reason this is not working is because you can’t get the local player in a normal script. To fix this you’re going to take the player as an argument from the Triggered Event.
Fixed Script:

local replicatedstorage = game:WaitForChild("ReplicatedStorage")
local gun = replicatedstorage.Guns:WaitForChild("AK47")

script.Parent.Humanoid.Died:Connect(function()
	print("dead")
	local clone = gun:Clone()
	clone.Parent = game.Workspace
	local l = script.Parent.Torso.Position
	clone:MoveTo(Vector3.new(l.X,l.Y,l.Z))
	clone.Handle:WaitForChild("TouchInterest"):Destroy()
	clone.Handle.ProximityPrompt.Triggered:Connect(function(player)
		clone.Parent = player.Backpack
		script.Parent:Destroy()
	end)
end)
2 Likes

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