Parts in Region not working

I have this script which basically adds a sensor called ARS (located in my trains) to a table. I’m programming this just to help with my next project with involves this script.

local Region = Region3.new(Vector3.new(-319.679, 4.1, -0.642), Vector3.new(0.471, 4.1, -0.642))

function GetPlayersInPart(player, part)
    local region = Region
    local partsInRegion = workspace:FindPartsInRegion3(region)
    local Sensors = {}
    for _,Part in pairs(partsInRegion) do
        local ARS = player.Character.Humanoid.SeatPart.Parent.ARS
        if ARS then
            table.insert(Sensors,ARS)
        end
    end
    return Sensors
end

GetPlayersInPart(Region)

In my output, it says Character isn’t a valid member of Region3 (name of the script).

You’re passing a Region3 to GetPlayersInPart, which seems to expect a Player Instance as its only used argument. What job do you actually want this function to fulfil? It seems to be just inserting an Instance into a table several times, based on how many parts are inside the region.

That’s what I need it to do. I need it to add it to a table and display it.

Do you understand that the function is currently identical to:

local workspaceService = game:GetService("Workspace")
local region = Region3.new(Vector3.new(-319.679, 4.1, -0.642), Vector3.new(0.471, 4.1, -0.642))

local function f(player)
    local ars = player.Character.Humanoid.ARS
    local t = {}
    for _ = 1, #workspaceService:FindPartsInRegion3(region) do
        table.insert(t, ars)
    end
    
    return t
end

I can see no use case for this.

Ok fine looks like i’ll be explaining.

Basically I need to learn Parts in Region because I have this Train Driving Game I’m working on and I’m scripting “Level Crossings” if you’re american or whatever you probably call them RailRoad Crossings.

Basically each coach or whatever in the game (train coach) will contain a part called ARS. The region is basically track through the Crossing. If ARS is in there it’ll lower and stay down until ARS has left the region.

image

1 Like

Just give the Crossing a part you’ll use to detect collision and give it a tag “Crossing” or something. Connect to it’s touched event. You can do this by connecting when the instance is added to the Workspace. Or by looping through GetTagged(Tag) and connecting that way. Anyways once its connected you can do some logic to see if anything is touching the part this should account for overlap as well.

No need to use regions. Though it’s totally possible.

If I had to use Connect events In the train community on Roblox we call them sensors then I’d have to make a lot of them for all the different lengths we have in the game. It’ll take a long time.

Using Region3 saves time as it stays lowered until every “ARS” has gone across.

I updated my post because I realized that your ARS part is on the carriage and not the crossing. Anywho you should use a part for the crossing that is large enough for the entire train to pass through. Kinda like when you check if a humanoid is touching a part. Just check if the collider is touching a train. You can give the train a tag called train and say HasTag(“Train”) or you can parse through the object to find a part that only a train has… with the latter being more CPU intensive.

Me and my friends (who own separate railway games) all agree on using Region3 for it.

And also the crossing is quite short, only 2 coaches can pass over it at a time and when I have trains with 16 coaches then…

Either way the part on touch and the region three is going to function the exact same way. The only difference is the part itself doesn’t need to be added to a list and use memory. But if you want to use Region3’s for the sake of learning how to use Region3’s that’s fine. That’s what this seems to be anyway. Nothing wrong with learning :smiley: I never accounted for answering the question about Region3’s so give me a moment while I go do that :stuck_out_tongue:

But if I were to use OnTouch, I’d have to have lots of sensors for different length trains and I don’t want to have to do that in case they respawn or something while going over.

You’d only need a single part on each Crossing, which if you reuse crossings could easily be incorporated in it. And if anything is inside the part that is a train, you’d basically use a debounce and set a variable to true… or without even using the variable cause the crossings barriers to drop. Likewise once nothing is detected or the touchended event fires you’d simply lift the barrier. All of the cars would have to go through the part. So the only downside that may happen is the barrier constantly try to go up when a car exits, and another car enters… That’s an easy fix though.

Cars aren’t even added so to be honest that wouldn’t matter. Would you mind helping me on discord?

I was referring to train cars sorry :stuck_out_tongue:

Ah right but do you want to help me on discord?

I’m probably just gonna fiddle around with your code and post it here. Just to be clear though, you wanted your script to detect when the train is in the region of the crossing and then activate the crossing right? Because your GetPlayersInRegion function doesn’t do that at all.

Yes basically. I have 2 separate scripts called LowerCrossing and RaiseCrossing so if you can just do LowerCrossing.Disabled = false and true and the same for RaiseCrossing Please :slight_smile:

As far as getting parts from a region3. I’ve used a function I found awhile ago on the Wiki. I can’t find it anymore so I can’t give credit unfortunately. I use it frequently on my terrain generation. Here’s the code.

local function CreateRegion3FromLocAndSize(Position, Size)
    local SizeOffset = Size/2
    local Point1 = Position - SizeOffset
    local Point2 = Position + SizeOffset
    return Region3.new(Point1, Point2)   
end

local function GetPartsInRegion(Position)
    local Region = CreateRegion3FromLocAndSize(Position, Vector3.new(50,10,10)) 
    return workspace:FindPartsInRegion3(Region, nil, 10) 
end

You can just loop through the table GetPartsInRegion returns and simply check if any of the parts belong to a train.

Vector3.new(50,10,10)

The train track I aligned this to was going from left to right which is why it’s 50 in the x direction. But if your train was going in the z you’d just change it to Vector3.new(10,10,50)

Yeah Im confused