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
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.
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.
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!