Wait wait wait. We gotta fix this code first.
It's never a good idea to give variables random, single letter names. Why? Well, let's say you wrote 100 lines of code (not even very much!), but you defined variables with random letters, the way you're doing here. Come back a month later to debug and you have absolutely no clue what's going on! You define b at line 5, but just a few lines later you'll be thinking, "da heck is b????" This is because none of the variable make sense. To figure out what they are, you have to look back through the code to see how they were defined, and then make sure they didn't change later.
That's a total pain in the butt to do.
Name variables something meaningful, something that represents what they equal. This is also super helpful for anyone helping you on this site. I don't have time to memorize random letters, but keeping track of a few English words is nice and simple.
Here's some names I might give your variables:
local part = script.Parent
function OnClicked()
print(part.Name)
local clone = part:clone()
clone.Parent = game.Workspace
clone.Position = clone.Position + Vector3.new(6,0,0)
local x = clone.Position.X
local z = clone.Position.Z
local name = "[".. x ..", ".. z .."]"
print(name)
end
part.ClickDetector.MouseClick:connect(OnClicked)
For your original question, it's super easy. We use the FindFirstChild method. This will search for a child, by name, and return it if it's found. Otherwise, it returns nil. It's best used in combo with if statements, so we can check if the child exists or not.
local part = script.Parent
function OnClicked()
print(part.Name)
local clone = part:clone()
clone.Parent = game.Workspace
clone.Position = clone.Position + Vector3.new(6,0,0)
local x = clone.Position.X
local z = clone.Position.Z
local name = "[".. x ..", ".. z .."]"
print(name)
if workspace:FindFirstChild(name) then
print("It's found!")
end
end
part.ClickDetector.MouseClick:connect(OnClicked)
1 comment
FindFirstChild is much easier and simpler.