Tool picking and throwing not working

I have 3 scripts here (probably could be shortened) which can make you equip any tool in a folder by clicking on it, that destroys the tool in the folder and gives the player the real one. My issue is that when I drop it I can’t equip it again.

Items (Module Script):

local module = {}

function module.EquipItem(Player, Item)
	if Item:IsA("Tool") then
		Item.Parent = Player.Character
		workspace.Tools:FindFirstChild(Item.Name):Destroy()
	end
end

function module.UnequipItem(Player, Item)
	if Item:IsA("Tool") then
		local newItem = Item:Clone()
		local newItemHandle = newItem.Handle
		newItemHandle.Parent = workspace.Tools
		newItemHandle.Name = newItem.Name
		newItem:Destroy()
		Item:Destroy()
		
		newItemHandle.Anchored = false
		newItemHandle.CanCollide = true
		
		for _, v in pairs(newItemHandle:GetDescendants()) do
			if not v:IsA("ClickDetector") and not v:IsA("SpecialMesh") then
				v:Destroy()
			end
		end
		
		local NewClickDetector = Instance.new("ClickDetector", newItemHandle)
	end
end

return module

ItemEquip (Script):

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local Items = require(ReplicatedStorage.Items)

workspace.Tools.ChildAdded:Connect(function()
	for _, Tools in pairs(workspace.Tools:GetChildren()) do
		if Tools:IsA("MeshPart") or Tools:IsA("BasePart") then
			Tools.ClickDetector.MouseClick:Connect(function(Player)
				local newTool = game.ReplicatedStorage:FindFirstChild(Tools.Name):Clone()
				Items.EquipItem(Player, newTool)
			end)
		end
	end
end)

ItemUnequip (LocalScript):

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local Player = Players.LocalPlayer
local Character = Player.CharacterAdded:Wait() or Player.Character

local Items = require(ReplicatedStorage.Items)

UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.G and Character:FindFirstChildOfClass("Tool") then
		local Tool = Character:FindFirstChildOfClass("Tool")
		
		Items.UnequipItem(Player, Tool)
	end
end)

I may be wrong because you haven’t shown the layout of your Tool but…

local NewClickDetector = Instance.new("ClickDetector", newItemHandle)

Your ClickDetector is parented to newItemHandle

Tools.ClickDetector.MouseClick:Connect(function(Player)

Shouldn’t this be Tool.Handle.ClickDetector?

Also…

workspace.Tools.ChildAdded:Connect(function()

ChildAdded passes the new child to the function

workspace.Tools.ChildAdded:Connect(function(NewTool)

So this could save you from looping through the whole folder every time something is added.

I do have it set to the Handle so it’s the same as Tool.Handle.ClickDetector

Changed it to this, not good at scripting lol so I don’t really know what to do in this case (first time trying to create such a system)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local Items = require(ReplicatedStorage.Items)

workspace.Tools.ChildAdded:Connect(function(Tool)
	if Tool:IsA("MeshPart") or Tool:IsA("BasePart") then
		Tool.ClickDetector.MouseClick:Connect(function(Player)
			local newTool = game.ReplicatedStorage:FindFirstChild(Tool.Name):Clone()
			Items.EquipItem(Player, newTool)
		end)
	end
end)

for _, Tools in pairs(workspace.Tools:GetChildren()) do
	if Tools:IsA("MeshPart") or Tools:IsA("BasePart") then
		Tools.ClickDetector.MouseClick:Connect(function(Player)
			local newTool = game.ReplicatedStorage:FindFirstChild(Tools.Name):Clone()
			Items.EquipItem(Player, newTool)
		end)
	end
end

(Tried debugging by adding a print on the childadded and when i drop an item it doesnt do anything erm)

if Tools:IsA("MeshPart") or Tools:IsA("BasePart") then

Tools isn’t a MeshPart or a BasePart - it’s a Tool… Change this to

if Tools.Handle:IsA("MeshPart") or Tools.Handle:IsA("BasePart") then

Where is the localscript stored?
I’ve always found that having UserInputService scripts in StarterPlayerScripts works a lot better than in StarterCharacterScripts

image
The items in the Tools folder are just cloned handles of the actual tools to act as props when they’re dropped.

it is stored in StarterPlayerScripts

Ahh ok I understand the structure/layout now so disregard my previous replies :laughing:
Ok, so the problem is your dropping the tool with a LocalScript so the ServerScript that’s monitoring the Tools Folder doesn’t see anything new being added.
You need to setup a RemoteEvent and pass the tool that’s being dropped so the Server can add it to the folder instead.

Add this line to the bottom of you SERVERSCRIPT

ReplicatedStorage.ToolEvent.OnServerEvent:Connect(Items.UnequipItem)

Change the InputBegan bit of code in your LOCALSCRIPT to this

UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.G and Character:FindFirstChildOfClass("Tool") then

		local Tool = Character:FindFirstChildOfClass("Tool")
		ReplicatedStorage.ToolEvent:FireServer(Tool)
		
	end
end)

Add a RemoteEvent in ReplicatedStorage and call it ToolEvent

Alr, is my brain fried or what, because in theory your script should work but I wouldn’t know because roblox decided to take a dump on my script and out of nowhere, the script doesn’t work and I get this error for no reasonnnnn

hehehe im losing my mind (:

local module = {}

function module.EquipItem(Player, Item)
	Item.Parent = Player.Character
	workspace.Tools:FindFirstChild(Item.Name):Destroy()
end

function module.UnequipItem(Player, Item)
	local newHandle = Item.Handle:Clone()
	newHandle.Parent = workspace.Tools
	
	Item:Destroy()
	
	newHandle.Anchored = false
	newHandle.CanCollide = true
		
	for _, v in pairs(newHandle:GetDescendants()) do
		if not v:IsA("ClickDetector") and not v:IsA("SpecialMesh") then
			v:Destroy()
		end
	end
		
	local NewClickDetector = Instance.new("ClickDetector", newHandle)
end

return module

Error:

ReplicatedStorage.Items:9: attempt to index nil with 'Handle'  -  Server - Items:9

Check your code - you may have changed something unintentionally.
I created a baseplate with your original code and my solution and it works as expected, I’ve attached the file so you can double check your own code with it.

ToolTest.rbxl (58.5 KB)

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