Is there a way to find a certain part in the workspace

Hi, essentially what I want to do is to find a VehicleSeat in the Workspace, however the vehicle seat’s location varies (It’s sometimes in a model in a model etc) and I can’t find a away around it. If you have any ideas how I could do this I’d appreciate them :slight_smile:

Thanks

Yes! It is actually very easy to do so, all you have to do is iterate though the descendants of workspace.

Run an for i, v in pairs loop through the descendants, and check for the name and the class of the object.

You can (but not recommended) loop through all descendants of the workspace, by doing so:

for _,v in pairs(workspace:GetDescendants()) do
	if v:IsA("VehicleSeat") then
		-- here u go
	end
end

Looping through the entire workspace would use tons of memory, I’d recommend you to put all the models that might contain the vehicles inside one folder, let’s say a folder named “Cars” inside workspace.

for _,v in pairs(workspace.Cars:GetDescendants()) do
	if v:IsA("VehicleSeat") then
		-- here u go
	end
end
3 Likes

Yup. I just gave a simple solution, though the one you’ve provided is better, because it won’t consume as much memory.

You can use CollectionService and tag the object (cooler)

Or you can use ObjectValue instances (easier)

I wouldn’t recommend looping through everything every time you want to access this object, that could become a performance issue

There are four ways I can think of. Other people stated three of these ways.


The first way is to assign it to a variable at the top of the script. If the VehicleSeat is inside of Workspace when the game starts, you’d put this in at the very top.

local vehicleSeat = workspace.VehicleSeat --this would be the vehicle seat throughout the script
--remember to change "VehicleSeat" to the name of it!

This is the simplist and easiest way to do this. Use WaitForChild() if you need to, and if you need this variable across multiple scripts, consider using a ModuleScript.


The second way is to use CollectionService and give the VehicleSeat a tag.

local collection = game:GetService("CollectionService")

collection:AddTag(vehicleSeat, "VehicleSeat")
--remember to change "vehicleSeat" to the actual seat!

To get the tagged seat, just do this.

collection:GetTagged("VehicleSeat")[1] --this would be the seat

I actually think it’s better to use the first way, though.


The third way is to use an ObjectValue, and assign the seat to the value. There’s not a whole lot to explain here.

To get the seat from the ObjectValue in a script, do this.

objectValue.Value --this would be the seat
--remember to change "objectValue" to the path to the actual ObjectValue!

The last, and unrecommended way to do this is to iterate over Workspace’s descendants and find the seat from there. Here’s an example of a function that returns the seat.

local function FindVehicleSeat()
    for index, seat in pairs(workspace:GetDescendants()) do
        if seat:IsA("VehicleSeat") and seat.Name == "VehicleSeatNameHere" then
            return seat
        end
    end
end
1 Like

as simple as this:

for i,instance in pairs(game.Workspace:GetDescendants()) do -- loops through literally every descendant of Workspace
    if instance.ClassName == "VehicleSeat" then
        --do stuff
    end
end

Edit: I didn’t consider that the instance could be named other than the default class name, so I made it simply check the ClassName instead.

1 Like
for i, v in pairs(game:GetService("Workspace"):GetDescendants()) do
if v.Name == "VehicleSeat" then
if v:IsA("VehicleSeat") then
if v.ClassName == "VehicleSeat" then
end
end
end
end

theres 3 ways

all the solutions here are overcomplicated

local vehicleSeat = workspace:FindFirstChild("VehicleSeat",true)
print(vehicleSeat)

note: this only works for the first instance of a vehicleseat, if there are 2 of them, it will only print 1
I think that’s what you want tho? I cant tell

my loop one was good, a loop isnt complicated plus it should be findfirstdecendent