How to locate a something in workspace

Hello so i am starting to learn some programming and i have always wanted to know how i would be able to achive a system like flood escape 2.

The system would be able to find any part(with a value inside/with a name) in workspace even if it is in a folder or in a group.Then it would be able to make it do diffrent stuff like kill and maybe spin and dissappear.

e.g
if I name a more than one part something like “_kill” then it would become a kill block(or insert a value in it with "_kil"l)
What I want to know most is how to locate the part anywhere in workspace either in a group in a model or another part and the make it do the function I need.

Currently the closest thing i can do to it right now is only :FindFirstChildWhichIsA()
but it won’t work because it only gets one.

I am not asking for scripts but i hope someone might teach me how to make it.

5 Likes

Have a variable that identifies the original kill part. Assuming that there are not any other siblings that have the same name.

If your script is inside the part, you can simply do:

local KillPart = script.Parent

Hope this helped.

Sorry if i didn’t make it clear

What i want to know is not to put a script inside the part but maybe in serverscriptservice and find it in workspace whether its in a model, folder or anything that it is in.

Having multiple parts named equally makes it difficult to distinguish which one you declared. Either you name them differently or add a value inside them. Use Instance:GetDescendants() in order to find descendants of something and match their name.

I don’t really get what you mean.
If i add a value could i still make all the value the same?

What I exactly meant was adding “tags” to memorize the order of the parts. Do not set all values the exact same value.

1 Like

How does flood escape 2 add a value and they are the same?

You lost me there. What exactly is your goal with the system?

Do you know flood escape 2?

When you make a map for flood escape 2 there are buttons and when a player presses a button they make stuff like blocks appear, disappear and a lot more. You can make them do all of those by inserting stringvalue into them and naming them stuff like _Appear,_Fall

You could use this kind of script construct. It searches through everything for tags and then applies their effects to the parts they’re in.

function TryApplyBehavior(item)
	if item:IsA("ObjectValue") and item.Name == "_KillBrick" and item.Parent:IsA("BasePart") then
		local part = item.Parent
		-- Set up kill brick behavior here.
	end
end

for _, item in ipairs(workspace:GetDescendants()) do
	TryApplyBehavior(item)
end
workspace.DescendantAdded:Connect(TryApplyBehavior)
3 Likes

Could you explain it for me? I want to learn.

This thing is run on every single object in the workspace, now and in the future.

This checks to make sure this object is a tag and it’s inside a part.

This is where your code would go.

These automatically run the function on everything in the workspace, including new objects that are added afterwards.

3 Likes

I think you forgot “do”
and how would you make the functions?

function TryApplyBehavior(item)
	if item:IsA("ObjectValue") and item.Name == "_KillBrick" and item.Parent:IsA("BasePart") then
		local part = item.Parent
		-- Set up kill brick behavior here.
	end
end

for _, item in ipairs(workspace:GetDescendants()) do
	do TryApplyBehavior(item)
end
workspace.DescendantAdded:Connect(TryApplyBehavior)

Something you should check out for systems like this is CollectionService. Making behaviours for certain assets is very easy with it. Define your logic for a certain tag and then apply it to various parts of your choosing.

You might still have dependencies in the beginning (hierarchical- and instance-wise) for the sake of iterating, but you can otherwise let one script handle the bulk of things.

2 Likes

I am a beginner and i have no idea what you mean.

How would you add stuff like kill?

This isn’t how Lua works. I would recommend learning basic syntax before trying to make anything as complicated as this.

3 Likes

AlvinBlox has an amazing collection of tutorials on YouTube (which is a great place to start if you’re looking to learn), he also has a video on how to create a kill brick with CollectionService as @ScriptingSupport suggested.

You can watch this here:

1 Like

To answer your initial question, FindFirstChild has a second parameter recursive which will iterate through all descendants. If the item exists, it is returned, otherwise nil.

local killPart = workspace:FindFirstChild("KillPart", true)

The above returns the ‘KillPart’ (assuming it exists), even if located within multiple folders or models.

1 Like

Hey, there are multiple ways you can get objects in the Workspace.

Get all objects with a certain name.
function GetAllObjectsWithName(name)
    local objects = {} -- List of objects.
    for i, v in pairs(workspace:GetChildren()) do
        if(v.Name == name) then -- Check if the name matches.
            table.insert(v) -- Insert the object into the table.
        end
    end
    return objects
end
Get first object with a certain name.
local object = workspace:FindFirstChild("[NAME]")

Like what you wanted:

Get all descendants with a certain name.
function GetAllObjectsWithName(name)
    local objects = {} -- List of objects.
    for i, v in pairs(workspace:GetDescendants()) do -- :GetDescendants returns all objects, even if they're in a folder, etc.
        if(v.Name == name) then -- Check if the name matches.
            table.insert(v) -- Insert the object into the table.
        end
    end
    return objects
end