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)
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)