a = script.Parent function OnClicked() print(a.Name) local b = a:clone() b.Parent = game.Workspace b.Position = b.Position + Vector3.new(6,0,0) local t = b.Position.X local u = b.Position.Z local v = "[".. t ..", ".. u .."]" print (v) end a.ClickDetector.MouseClick:connect(OnClicked)
I want to know if there is anything in the workspace with the same name as v
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)
a = script.Parent function OnClicked() print(a.Name) local b = a:clone() b.Parent = game.Workspace b.Position = b.Position + Vector3.new(6,0,0) local t = b.Position.X local u = b.Position.Z local v = "[".. t ..", ".. u .."]" print (v) end a.ClickDetector.MouseClick:connect(OnClicked)
Okay, so if you want to add something that checks everything in workspace if it has a specific name, we would use a pairs loop and if statement. We will use the pairs loop to check everything in workspace.
for i,p in pairs (workspace:GetChildren()) do -- Check all children of workspace. end
But all this will do is get everything in workspace, if we want to check if it has a specific trait, we will have to use an if statement.
for i,p in pairs (workspace:GetChildren()) do if p.Name == v then -- Check if the name of p (the child we're checking) has the same name as v. print("I found something with v's name") else -- if it doesn't... print("Unsuccessful , still finding") end end
So if we want to put this all into one statement added onto your onclicked, we will do this.
a = script.Parent function OnClicked() print(a.Name) local b = a:clone() b.Parent = game.Workspace b.Position = b.Position + Vector3.new(6,0,0) local t = b.Position.X local u = b.Position.Z local v = "[".. t ..", ".. u .."]" print (v) for i,p in pairs (workspace:GetChildren()) do if p.Name == v then -- Check if the name of p (the child we're checking) has the same name as v. print("I found something with v's name") else -- if it doesn't... print("Unsuccessful , still finding") end end end a.ClickDetector.MouseClick:connect(OnClicked)