Hi guys, I’m a person which just started scripting and I need some help with this problem, since I can’t find a solution to the problem anywhere.
So I want to make this pick-up script work even when it got picked up once, then dropped again by pressing backspace. When you do that, the script breaks and it can be picked up just by touching it instead of clicking on it.
I’ve looked everywhere for a solution but I didn’t find any. It’s kind of weird that I can’t find an answer to this anywhere on the internet, because the issue is probably so common! I even tried to fix it but for some reason it didn’t work at all so I quit fixing it.
Anyways, here’s the script:
while true do
wait(0.0001)
script.Parent:WaitForChild("TouchInterest"):Destroy()
script.Parent.Parent.ClickDetector.MouseClick:Connect(function(Player)
print("picked up")
local tool = script.Parent.Parent
tool.Parent = Player:WaitForChild("Backpack")
print ("the object was picked up by", (Player.Name))
script.Parent.Parent.ClickDetector.Parent = game.ServerStorage
end)
end
But just as an aside, the normal wait function has a minimum of approximately 0.3 seconds so for making it any smaller you will have to use run service. And if you want to use the minimum wait the simply leave the number argument empty, like this: wait(). I know this is all really off topic but I thought I point that out.
What I was talking about is that with that script, when you play the game, when you click on the part, you pick it up, which is intended. But when you drop it (AKA press backspace), the tool will be dropped. But now this time if you touch the object, you automatically pick it up. Which isn’t intended. I want the tool to be picked up by only clicking on it, not touching it with my character. Another problem is when it’s dropped, you can’t click the tool to pick it back up anymore.
script.Parent.ChildAdded:Connect(function(child) -- triggers when new child is added
if child.Name == "TouchInterest" then -- check if child is TouchInterest
child:Destroy() -- if so, destroy
end
end)
Combined:
local tool = script.Parent.Parent
script.Parent.ChildAdded:Connect(function(child)
if child.Name == "TouchInterest" then
child:Destroy()
end
end)
script.Parent.Parent.ClickDetector.MouseClick:Connect(function(Player)
print("picked up")
tool.Parent = Player:WaitForChild("Backpack")
print ("the object was picked up by", Player.Name)
script.Parent.Parent.ClickDetector.Parent = game.ServerStorage
end
Hey there, it appears the issue is due to the way you handle your ClickDetector, I recommend instead of having a pre-existing clickdetector inside the tool, and then moving it into ServerStorage, to instead create a ClickDetector whenever the tool is dropped, and then destroying it when it gets picked up.
Something like this should work:
local Handle = script.Parent
local Tool = Handle.Parent
Tool.Unequipped:Connect(function() -- This event is fired whenever the tool is unequipped, however dropping does also fire this.
if Tool.Parent == workspace then
-- The tool was dropped, and not just unequipped.
Handle:WaitForChild("TouchInterest"):Destroy()
local CD = Instance.new("ClickDetector",Tool) -- Create a new ClickDetector inside the Tool
CD.MouseClick:Connect(function(Player)
CD:Destroy()
Tool.Parent = Player:WaitForChild("Backpack")
end)
end
end)
It didn’t work. In the output it says:
Something unexpectedly tried to set the parent of TouchInterest to NULL while trying to set the parent of TouchInterest. Current parent is Handle.
Maybe I put that script in the wrong place? This is how I put your script:
script.Parent.ChildAdded:Connect(function(child)
if child.Name == "TouchInterest" then
child:Destroy()
end
end)
script.Parent:WaitForChild("TouchInterest"):Destroy()
script.Parent.Parent.ClickDetector.MouseClick:Connect(function(Player)
print("picked up")
local tool = script.Parent.Parent
tool.Parent = Player:WaitForChild("Backpack")
print ("the object was picked up by", (Player.Name))
script.Parent.Parent.ClickDetector.Parent = game.ServerStorage
end)