I would like to know how I could get the exact object I clicked in studio
Heres My hierachy and script(the script is in the Crate Script)
Orbs:WaitForChild("Crate").ClickBox.ClickDetector.MouseClick:Connect(function(selectedCrate)
selectedCrate.CrateNum.Value = 1
if selectedCrate.CrateNum.Value == 1 then
if selectedCrate.Spawn.Value == false then
if isBreaking.Value ~= true then
if plr.Values.Power.Value == "0" then
isBreaking.Value = false
else
isBreaking.Value = true
currentBreak.Value = selectedCrate.Name
game.ReplicatedStorage.Orbs:WaitForChild("Crate"):FireServer(plr)
while wait() do
Orbs.DescendantRemoving:Connect(function(part)
if part.Name == "Crate" then
isBreaking.Value = false
currentBreak.Value = " "
end
end)
end
end
end
print("sent")
elseif selectedCrate.Spawn.Value == true then
print("Still spawning")
end
else
print("Not Right Crate ")
end
end)
You already know which crate was clicked, because it’s the one you connected the event to.
I think you might just be confused about how WaitForChild works.
Your script will only connect to the click event for a single Crate.
What I think you actually want is connect a click event to each crate in the folder.
You can do that with a simple for loop:
-- this function is called whenever a crate is clicked
local function OnCrateClicked(player, crate)
-- inside this function, `crate` refers to the Model that was clicked
-- and `player` refers to the player that clicked it.
-- ... do anything you want with the crate here.
end
-- this function is called once for each crate at the start of the game
-- and once for each crate added to the folder during the game
local function ConnectCrateClick(crate)
crate.ClickBox.ClickDetector.MouseClick:Connect(function(player)
OnCrateClicked(player, crate)
end)
end
-- connect one click event for each crate in the folder:
for _, crate in pairs(workspace.OrbsFolder) do
ConnectCrateClick(crate)
end
-- we also probably want to connect clicked events for any crates
-- that get added later:
workspace.OrbsFolder.ChildAdded:Connect(ConnectCrateClick)
Let me know if that’s not what you were asking, I had to do some interpretation of your question