Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Car removal script - needs slight editing?

I have this script for a car removal that's on a timer right (with a billboard GUI) it shows the time until the cars removed but I was wondering if someone could help with the script to where if the user puts their car in park (P) that it cancels out the script so it doesn't proceed to do the removal countdown and remove the car.

local seat = script.Parent -- whenever i say seat, i mean script.Parent
local model = script.Parent.Parent -- same as above
local warngui = script.warning -- & again
warngui.Parent = script.Parent -- this puts the billboardgui in the seat
local waittime = 5 

seat.ChildRemoved:connect(function(x) -- once someone leaves, we call it x
	if x.Name == "SeatWeld" then -- seatweld is there when someone is in the car
		warngui.Enabled = true -- makes billboard gui visible
		warngui.text.Text = ""..waittime.." seconds until removal"
		-- ..waittime.. basically puts the waiting time in the text
		repeat wait(1) -- every 1 second do this
		waittime = waittime - 1 -- yea
		warngui.text.Text = ""..waittime.." seconds until removal"
		until waittime == 0 or seat:findFirstChild("SeatWeld")
		-- until person has no more time, or until person comes back in the car
		waittime = 5 -- resets wait time 
		warngui.Enabled = false -- makes billboard gui invsibile
		if seat:findFirstChild("SeatWeld") == nil then -- if no1 in the car
			model:Destroy() -- bye :(
		end
	end
end)

2 answers

#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


-- Services ~

local CollectionService = game:GetService("CollectionService")

-- Tables ~

local ParkingSpots = {}
local PartsToIgnore = {} -- All the parts you want to ignore in the region, you can also use :GetChildren() or :GetDescendants() since they all return tables

-- Values ~

local waittime = 5

-- Object Variables ~

local seat = script.Parent
local model = script.Parent.Parent

local warngui = script.warning
warngui.Parent = script.Parent

-- Functions ~

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

0

idk if he meant the state of being parked or a literal parking spot

0

ikr

0

The car is in park by the user pressing p on their keyboard. not a parking spot as that could literally be anywhere.

0

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.