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