Trying to organize my models

I made a script that picks up tools from models that are already present on the baseplate but recently I tried to organize the models a bit by putting them, including the tools, into a folder. The models went into a folder called “Models” and the tools in ReplicatedStorage went into a folder called “Weapons/Items” that is still in ReplicatedStorage. There’s no error in the output, nothing. Any idea why this might be?

local player = game:GetService("Players")

local tools = game.ReplicatedStorage["Weapons/Items"]
local toolModels = game.workspace["Models"]

local clickDetector = {
	toolModels["Baseball Bat"].Inetract.ClickDetector,
	toolModels["Pistol"].Inetract.ClickDetector
	}

clickDetector.MouseClick:Connect(function(plr)
	tools.Parent = plr:WaitForChild("Backpack")
end)
3 Likes

Just noticed I misspelt “Interact” but that wasn’t the problem. Still can’t pick up the items and the output still shows nothing.

Why is the click detectors in a table?
You should loop through each element in the table if you want to access them.
Also you should clone from the replicated storage instead of directly accessing the objects, so the original tool stays untouched.

I’m still new to scripting so I’m not really sure how a for loop works. I made this change you said but now clickDetector.MouseClick:Connect(function(plr) is outlined in red and saying “unknown global’clickDetector’”

local player = game:GetService("Players")

local tools = game.ReplicatedStorage["Weapons/Items"]
local toolModels = game.workspace["Models"]

for clickDetector in
	toolModels["Baseball Bat"].Interact.ClickDetector,
	toolModels["Pistol"].Interact.ClickDetector do
end
 
clickDetector.MouseClick:Connect(function(plr)
	local cloneTools = tools:Clone()
	tools.Parent = plr:WaitForChild("Backpack")
end)

There are a couple of errors in this script.

  1. local toolModels = game.workspace["Models"]
    this line returns the model object, not a table with the children, for that, you want game.workspace[“Models”]:GetChildren()

  2. clickDetector.MouseClick:Connect(function(plr)
    clickDetector is a table of click detector, it is not a click detector so the .MouseClick event won’t work on it. What you want to do is loop through the table and connect the event individually.

Thats not what I meant here.

Let me fix the code for you.

local tools = game.ReplicatedStorage["Weapons/Items"]:GetChildren()
local toolModels = game.workspace["Models"]

local clickDetectors = {
	toolModels["Baseball Bat"].Interact.ClickDetector,
	toolModels["Pistol"].Ineract.ClickDetector
	}

for _, clickDetector in ipairs(clickDetectors) do
    clickDetector.MouseClick:Connect(function(plr)
--you need to loop through every element here and assign the cloned tool to the backpack
-- OR clone the tool you want depending on the toolname
        tools.Parent = plr:WaitForChild("Backpack")
    end)
end

I’m still confused about the cloning part. Once again, bear with me, I’m pretty new to this.

-- Assume you have a part in the workspace that you want to clone
local originalObject = game.workspace.ObjectToClone

-- Clone the object
local clonedObject = originalObject:Clone()

-- Parent the cloned part to the workspace or another desired parent
clonedPart.Parent = game.workspace

Well I ended up with this and now it works but gives me both items, at the same time, no matter of which I click.

local tools = game.ReplicatedStorage["Weapons/Items"]:GetChildren()
local toolModels = game.workspace["Models"]

local clickDetectors = {
	toolModels["Baseball Bat"].Interact.ClickDetector,
	toolModels["Pistol"].Interact.ClickDetector
}

for _, clickDetector in ipairs(clickDetectors) do
	clickDetector.MouseClick:Connect(function(plr)
		for _, tool in pairs(tools) do
			tool:Clone().Parent = plr:WaitForChild("Backpack")
		end
	end)
end

Please check your code and try to understand it before posting again.

You are looping through every tool in the folder tools and assigning it to the backpack.
If you only want it to correspond to the item it represents, do this instead:

local tools = game.ReplicatedStorage["Weapons/Items"]:GetChildren()

local ClickDetectors = {
    ["Baseball Bat"] = game.workspace["Models"]["Baseball Bat"].Interact.ClickDetector,
    ["Pistol"] = game.workspace["Models"]["Pistol"].Interact.ClickDetector
}

for toolName, clickDetector in pairs(ClickDetectors) do
    clickDetector.MouseClick:Connect(function(plr)
        local tool = tools[toolName]
        if tool then
            tool:Clone().Parent = plr:WaitForChild("Backpack")
        end
    end)
end

Well, I am trying to understand the code but I just tried yours and it doesn’t work. No outputs or anything either.

Code should be fixed. Let me know if there are any further issues.