Garage Door for Car Customization System

Hey guys! I’m trying to make a garage door that opens if I’m inside a car, for a car customization system, I would like to ask your opinion on the best way to do this.

I made a very confusing script… (I’m new with lua)

local Door = script.Parent.Parent.ExternGate
local IsTouching = false

game.Players.PlayerAdded:Connect(function(Player) --Well, I wanted some other way to get the player but idk how
	local Character = Player.Character or Player.CharacterAdded:Wait()
	
	for i,v in pairs(Character:GetChildren()) do --Do I create a script in the car or in the player to make it easier?
		if v:IsA("BasePart") then
			v.Touched:Connect(function(hit)
				if hit:IsA("VehicleSeat") then
					IsTouching = true
					
					script.Parent.Touched:Connect(function(hitt) --IDK what I'm doing
						if hitt == v and IsTouching == true then
							script.Parent.CanTouch = false
							for i = 3,(Door.Size.z / 0.15) do
								Door.CFrame = Door.CFrame - (Door.CFrame.lookVector * 0.15)
								wait()
							end

							wait(5)

							for i = 3,(Door.Size.z / 0.15) do
								Door.CFrame = Door.CFrame + (Door.CFrame.lookVector * 0.15)
								wait()
							end

							wait()

							script.Parent.CanTouch = true
                            --Does that make sense?
						end		
					end)	
				else
					IsTouching = false
				end
			end) 
		end
	end
end) --Sorry
1 Like

If this is just a script for opening/closing a garage door when the player’s character is seated in a VehicleSeat then I’d consider doing the following instead.

for _, Seat in ipairs(workspace:GetDescendants()) do
	if Seat:IsA("VehicleSeat") then
		Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
			if Seat.Occupant then
				--Open garage door.
			else
				--Close garage door.
			end
		end)
	end
end

https://developer.roblox.com/en-us/api-reference/property/VehicleSeat/Occupant

The value of VehicleSeat.Occupant is either the humanoid instance of the player’s character currently seated or nil (if unoccupied).

1 Like

Very Good, but, how can I make it if the player touches the detector to open the door?

Like this?

local Door = script.Parent.Parent.ExternGate

function JAJAJA()
	for _, Seat in ipairs(workspace:GetDescendants()) do
		if Seat:IsA("VehicleSeat") then
			Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
				if Seat.Occupant then
					for i = 3,(Door.Size.z / 0.15) do
						Door.CFrame = Door.CFrame - (Door.CFrame.lookVector * 0.15)
						wait()
					end
				else
					for i = 3,(Door.Size.z / 0.15) do
						Door.CFrame = Door.CFrame + (Door.CFrame.lookVector * 0.15)
						wait()
					end
				end
			end)
		end
	end
end

script.Parent.Touched:Connect(JAJAJA)

You can’t really have the player seated and touching a part at the same time, I’d recommend using a “ProximityPrompt” instance or a “ClickDetector” instance while the player is seated to achieve this.

https://developer.roblox.com/en-us/api-reference/class/ProximityPrompt

1 Like