Making an RPG where NPCs drop tools onto the ground for the player to pick up. For some reason whenever I try to clone a tool into Workspace it only works when i clone the handle of the tool but then the player can not pick it up. also i am using lootplan
local tool = script.Parent.Parent
local function onPartTouched(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
local Player = humanoid.Parent.Name
local Players = game:GetService("Players")
if humanoid then
tool.Parent = Player.Backpack
end
end
tool.Handle.Touched:Connect(onPartTouched)
Here is the script I am using for lootplan
local LootPlan = require(game.ReplicatedStorage.LootPlan)
local ItemPlan = LootPlan.new("single")
-- The first argument is the name, the second is the chance
ItemPlan:AddLoot("Apple of Healing",99)
ItemPlan:AddLoot("Nothing",1)
local Humanoid = script.Parent.Humanoid
local ItemType = ItemPlan:GetRandomLoot()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
function Dead()
if ItemType ~= "Nothing" then
local tool = ReplicatedStorage.Tools:FindFirstChild("Apple of Healing")
local item = tool:Clone()
local apple = item.Handle
local workspace = game:GetService("Workspace")
item.Parent = workspace -- parent it to the game environment
apple.Position = Humanoid.Parent.HumanoidRootPart.Position -- set it's position to the of the HRP. If NPC has no HRP, then set it to another Part of the NPC
end
end
Humanoid.Died:Connect(Dead)
The first script is in the tool and the second is in the NPC. Both server scripts
Player is actually the character in the first script.
One thing I noticed was your pickup code, which is trying to index the Backpack from the character model rather than the player itself. Here’s the fixed version:
local tool = script.Parent.Parent
local function onPartTouched(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if not humanoid then return end
local Player = game.Players:FindFirstChild(humanoid.Parent.Name)
if Player then
tool.Parent = Player.Backpack
end
end
tool.Handle.Touched:Connect(onPartTouched)
Now it says this
The Parent property of Workspace is locked, current parent: Sword Simulator v1.05, new parent Backpack - Server - Pickup:12
Instead of using this,
local Player = game.Players:FindFirstChild(humanoid.Parent.Name)
try using this:
local Player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
Try to also add in some print()
statements to check for things abnormal.
So i tried the stuff you said and it seems the problem is it isn’t finding the player because its not printing “Found Player” in the output
local tool = script.Parent.Parent
local function onPartTouched(otherPart)
print ("Touched")
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if not humanoid then return end
local Player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if Player then
tool.Parent = Player.Backpack
print("Found Player")
end
end
tool.Handle.Touched:Connect(onPartTouched)
1 Like
Delete your entire “Touched” script, trust me…
I have a solution and frankly its quite simple…
Just place the tool where you want it and check the box on the “Can be dropped” property of the tool. (I’m pretty sure thats the property.) This is not suitable if you do not want people to be able to drop the tool.