Destroying a part in a model from a Local Script(PAYING 200 ROBUX)

I have 4 models in replicated storage named LootTools1 to LootTools4 and they all have 2 apple parts with ClickDetectors in them. What I want is for my local script to find the model then destroy the part if i click it.
Screenshot 2023-11-07 at 9.49.40 PM

Current Local Script

-- Replicated Storage where the models are stored
local replicatedStorage = game:GetService("ReplicatedStorage")

-- Specify the models to target
local modelsToTarget = {
	replicatedStorage.LootTools1,
	replicatedStorage.LootTools2,
	replicatedStorage.LootTools3,
	replicatedStorage.LootTools4
}

local function onToolClick(player, part)
	if part.Name == "ApplePickUp1" or part.Name == "ApplePickUp2" then
		part:Destroy()
		print("Destroyed: " .. part.Name) -- Print the name of the destroyed part
	end
end

local function setupClickDetector(model)
	local clickDetector = model:FindFirstChild("ClickDetector")
	if clickDetector then
		clickDetector.MouseClick:Connect(function()
			for _, part in pairs(model:GetChildren()) do
				onToolClick(game.Players.LocalPlayer, part)
			end
		end)
	end
end

for _, model in pairs(modelsToTarget) do
	setupClickDetector(model)
end
1 Like

thanks for the help in advance!

ClickDetector can not be child of a model !

1 Like

the CD is the child of the part.

It doesnt seem to be a child of the part if you are looking for it inside the Model:

local function setupClickDetector(model)
	local clickDetector = model:FindFirstChild("ClickDetector")

If the CD is inside a part inside the model, then change that line, model.Part:FindFirstChild("ClickDetector")

@WxxMayN is right

my friends changed it up for me this is where im at, the part still doesnt destroy when i click it.

local player = game.Players.LocalPlayer

local function onToolClick(part)
    if part.Name == "ApplePickUp1" or part.Name == "ApplePickUp2" then
        part:Destroy()
        print("Destroyed: " .. part.Name) -- Print the name of the destroyed part
    end
end

-- Function to set up ClickDetectors for specified parts
local function setupClickDetectorsInParts(parts)
    for _, part in pairs(parts) do
        local clickDetector = part:FindFirstChild("ClickDetector")
        if clickDetector then
            clickDetector.MouseClick:Connect(function()
                onToolClick(part)
            end)
        end
    end
end

-- Replicated Storage where the models are stored
local replicatedStorage = game:GetService("ReplicatedStorage")

-- Specify the models to target
local modelsToTarget = {
    replicatedStorage.LootTools1,
    replicatedStorage.LootTools2,
    replicatedStorage.LootTools3,
    replicatedStorage.LootTools4
}

-- List to store parts with ClickDetectors
local partsWithClickDetectors = {}

-- Iterate through the models and gather parts with ClickDetectors
for _, model in pairs(modelsToTarget) do
    for _, part in pairs(model:GetChildren()) do
        table.insert(partsWithClickDetectors, part)
    end
end

-- Call the function to set up ClickDetectors for specified parts
setupClickDetectorsInParts(partsWithClickDetectors)

is there a way to get get the 2 parts from the model depending if its 1 2 3 or 4? i think after i get that done solving the click detector problem is easy.

And what output says? I think theres something wrong with the setup, your friend types code like an AI xD

1 Like

no he was working on it for me before this post, he just sent it. theres no output btw.

1 Like

should i send a studio copy so u can see the problem im having?

sure if you wish, I will check it when I have time

1 Like

Local Script Test.rbxl (62.6 KB)

Ill actually give 200 Robux To Someone who can help solve this.

This is still unsolved giving 200 to the person who can solve it. what i want is 4 random models and only one that spawns into workspace which i have already then the parts in the model when you click them they destroy for only the person who clicked it so through a local script.

Could you show me a screenshot of the parts inside the model.

Try something like this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

--// Models (I don't recommend doing it this way but okay)
local modelsToTarget = {
	ReplicatedStorage.LootTools1,
	ReplicatedStorage.LootTools2,
	ReplicatedStorage.LootTools3,
	ReplicatedStorage.LootTools4
}

for i,v in pairs(modelsToTarget) do
    for i2,v2 in pairs(v:GetChildren()) do -- I know..
        if v2:FindFirstChild("ClickDetector") then
            print("DEBUG | ClickDetector found on object: ",v2.Name,"|",v2:GetFullName())
            local ClickDetector = v2:FindFirstChild("ClickDetector")
            local PartName = v2.Name
            ClickDetector.MouseClick:Connect(function()
                print("DEBUG | Part clicked! | Part name:",PartName)
                if PartName == "ApplePickUp1" or PartName == "ApplePickUp1" then
                    print("DEBUG | Apple clicked, Destroying! | Part name:",PartName,"|",v2:GetFullName())
                    v2:Destroy()
                    print("DEBUG | Apple Destroyed")
                end
            end)
        end
    end
end

I cannot test it myself but i think this should work.
Let me know if it doesn’t and let me know what it shows in the output! :+1:

@DemonicPandazYT Did this work correctly?

It can actually. Its really handy since its applied to every part of the model

Actually it is very easy.

u set primary part for every single model,then set clicker to parimary part. if click,then destory the model,problem solved.