#What Do You Need?
We need region3
#What is a Region3?
Region3 is a data type that describes a volume in 3D space similar to an axis-aligned rectangular prism. It is commonly used with Terrain functions and functions that detect parts within a volume, such as Workspace:FindPartsInRegion3
#How can you use a Region3
In a region3 you can detect if certain objects are within the volume of the region.
Here's a short script demonstrating how it functions, keep in mind this is not related to your car script.
Make an empty baseplate, then make a big part that has a rich volume area, set the parts can-collide attribute to false, and make the parts transparency = 0.6
Name it something that differentiates to the other instances parented to the workspace.
Make a LocalScript parented to the StarterPlayerScripts Folder.
We'll make a script that detects if a player is within the region.
local Region = Region3.new(Vector3Min, Vector3Max)
What's this? This creates a new region including to parameters; "Minimum Vector, Max Vector"
But...
This does not reference the Region you inserted?? Let me show you how
local RegionPart = game.Workspace.Part
local Region = Region3.new(RegionPart.Position - (RegionPart.Size / 2), RegionPart.Position + (RegionPart.Size / 2))
There! Now we've got the volume within the region! Let's start detecting stuff within the region!
local RegionPart = game.Workspace.Part
local Region = Region3.new(RegionPart.Position - (RegionPart.Size / 2), RegionPart.Position + (RegionPart.Size / 2))
while wait() do
local PartsInRegion = workspace:FindPartsInRegion3(Region, nil, 1000)
end
This is a bit confusing at first but it's simple;
Param1: Which region do we want to specify; The region we created
Param2: What Parts should we ignore; None so far, since we're in an empty baseplate.
Param3; What is the maximum allowance of parts in the region; 1000
Great, next part!
local RegionPart = game.Workspace.Part
local Region = Region3.new(RegionPart.Position - (RegionPart.Size / 2), RegionPart.Position + (RegionPart.Size / 2))
while wait() do
local PartsInRegion = workspace:FindPartsInRegion3(Region, nil, 1000)
for i, hit in pairs(PartsInRegion) do
if hit.Parent:FindFirstChild("Humanoid") ~= nil then
print("Player found in region. Name: " .. hit.Parent.Name)
end
end
end
And yeah, you're probably and advanced script so you know about the next xD.
Great! Now it should work, check it out in studio it's working congrats! Your first Region3!!
You can also do many stuff like make the player take damage!;
local RegionPart = game.Workspace.Part
local Region = Region3.new(RegionPart.Position - (RegionPart.Size / 2), RegionPart.Position + (RegionPart.Size / 2))
while wait() do
local PartsInRegion = workspace:FindPartsInRegion3(Region, nil, 1000)
for i, hit in pairs(PartsInRegion) do
if hit.Parent:FindFirstChild("Humanoid") ~= nil then
print("Player found in region. Name: " .. hit.Parent.Name)
local Char = hit.Parent
Char.Humanoid:TakeDamage(100)
end
end
end
#Compatibility With Your Script
Make some parts all positioned to the parking spot;
Positions: Parking Spots
Sizes: Size of the parking lot
CanCollide: False
Transparency: 0.5 -- You can change this to 1 when your game releases.
Material: "ForceFeild"
Then parent all the regions to the
Okay, this is the hard part! We'll do it tho
Make a region in every parking lot you have.
Put a folder in workspace named ParkingRegions and parent all the regions to the folder
local CollectionService = game:GetService("CollectionService")
local ParkingSpots = {}
local PartsToIgnore = {}
local waittime = 5
local seat = script.Parent
local model = script.Parent.Parent
local warngui = script.warning
warngui.Parent = script.Parent
function Initialize()
for _, v in pairs(workspace.ParkingRegions:GetChildren()) do
local found
if v:IsA("BasePart") then
found = true
CollectionService:AddTag(v, "ParkingSpots")
table.insert(ParkingSpots, v)
else
found = false
end
end
end
Initialize()
function CheckRegion()
local ParkingSpots = CollectionService:GetTagged("ParkingSpots")
for regionIndex, region in pairs(ParkingSpots) do
local IndividualRegion = Region3.new(region.Position - (region.Size / 2), region.Position + (region.Size / 2))
while wait() do
local PartsFound = workspace:FindPartsInRegion3WithIgnoreList(IndividualRegion, PartsToIgnore, math.huge)
for hitIndex, hit in pairs(PartsFound) do
if hit:IsDescendantOf(model) then
return true
else
return false
end
end
end
end
end
seat.ChildRemoved:connect(function(x)
local Parked = CheckRegion()
if x.Name == "SeatWeld" and not Parked then
warngui.Enabled = true
warngui.text.Text = waittime.. " seconds until removal"
while not Parked do
wait(1)
waittime = waittime - 1
warngui.text.Text = ""..waittime.." seconds until removal"
if waittime <= 0 then
waittime = 5
warngui.Enabled = false
if seat:findFirstChild("SeatWeld") == nil then
model:Destroy()
end
end
end
end
end)
I'm too tired to explain, I'll edit it tomorrow and I'll explain how it works;
Sincerely - CaIcuIati0n
I can proudly say this works, if it does not work you have not listened or etc.
STAY SAFE :D
4 comments
idk if he meant the state of being parked or a literal parking spot
ikr
The car is in park by the user pressing p on their keyboard. not a parking spot as that could literally be anywhere.
You could've mentioned that, I'm very sorry it did not match your wish. Aside from that this would be a great implementation in your game.