Destroying a part in a model from a Local Script Click Detector(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.

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
3 Likes

Local Script Test.rbxl (62.6 KB)

here is the testing place for this.

According to roblox documentation: ClickDetector | Documentation - Roblox Creator Hub

“ClickDetector events fire on both the client and the server. Since a LocalScript will only run if it descends from a Player or player Character”

You’ll have to use some RemoteEvents on the serverside to get the client to remove it.

i would give you some code here that you could put into your game, but i couldn’t understand how your code was structured in your testing place

1 Like

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.

I don’t understand what you want, do you want to destroy the part for all players?

no so what i want is when you click the part that spawns in one of the 4 random models the local script in startercharacterscripts knows which one you click and destroys it locally so only for the one player.

So you want four parts to spawn when you click another part? Then destroy the one you clicked?

kind of so to give you context there are 4 models in each of the models there is 2 apples 2 cookies and 1 bloxy cola you can click each one for 1 tool but I just want it to destroy for the player locally.

just destroying the part locally and the script finding the right model to destroy the parts in is my only problem.

Then just run the Destroy method on that part inside a local script? You can use a ClickDetector or Proximity Prompt to get the part clicked on

i want it to be a click detector through client side but the problem is one of the 4 models spawns and I dont know how to make the local script know which model spawned so that it can find the clickable part to destroy after its clicked.

Then put a local script inside the StarterCharacterScripts and put the following,

local Models = game.Workspace.<Your Models Folder>

for _, Model in pairs(Models:GetChildren()) do
	if Model["ClickDetector"] then
		Model["ClickDetector"].MouseClick:Connect(function()
			Model:Destroy()
		end)
	end
end

the 4 models are in replicated storage though and only 1 randomly spawns per game so the local script needs to find the model with all the click parts in them.

all of them with different names

Are you spawning them locally, for only one player?

and i want them to click the part in the model for the part to destroy not the whole model.

im spawning them for the whole server but picking them up is local.

You need to clarify what you want more specifically, use grammar because I do not understand half of your sentences, also include your model structure, and model example using an image or a list of what is in your model, then explain how you spawn them, or if you even do and if it is local or server sided.

if u dont understand then ill pay someone else who does? I literally gave a link to the studio test server and it has everything in there.

this is the studio server link it has everything.