How do I detect a part colliding with other part?

hiii, I’m in process of remaking my door system into vehicle station system which allows players to spawn certain vehicle infront of the station.

The issue I have is after you spawn a car it doesn’t prevent you from spawning another one inside the previous one. I tried using :GetTouchingParts() to detect is anything in the spawning hitbox however it didn’t really work.

How can I prevent spawning second car if there are any parts inside the “Colision” part which I use as a hitbox?

Here is the code, parts which wouldn’t be useful were purposely removed

local ServerScriptService = game:GetService("ServerScriptService")
local VehicleStorage = game:GetService("ServerStorage"):WaitForChild("Vehicles")
local TweenService = game:GetService("TweenService")
local CollectionService = game:GetService("CollectionService")
local TaggedStations = CollectionService:GetTagged("VehicleStation")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundSystem = game.Workspace:WaitForChild("_SOUND_SYSTEM").DoorSounds
local RepairEvent = ReplicatedStorage:WaitForChild("Events").SystemOverrideEvent
local NotifyEvent = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("Notify")
--\\\:  SFX
local KeycardGrantedSFX = SoundSystem.Keycard_Accepted
local KeyCardDeniedSFX = SoundSystem.Keycard_Denied


function SpawnVehicle(Vehicle, newPosition)
	if VehicleStorage:FindFirstChild(Vehicle) ~= nil then
		local newVehicle = VehicleStorage:FindFirstChild(Vehicle):Clone()
		newVehicle:SetPrimaryPartCFrame(newPosition)
		newVehicle.Parent = game.Workspace.MapElements.Vehicles
		print("Vehicle Spawned")
		return true
	else
		warn("Invalid Vehicle")
		return false
	end
end



for _, Station in pairs(TaggedStations) do
	local SoundBlock = Station.Model.Screen
	local ProximityPrompt = Station.Trigger.ProximityPrompt
	local KeycardAccess = Station.KeycardAccess
	local KeycardReader = Station.KeyCardReader1
	local VehicleType = Station.VehicleType.Value
	local VehiclePosition = Station.VehiclePosition
	local Ready = true
	
	local function Triggered(Player)
		if not Ready then
			return
		end
		Ready = false

		

		if ProximityPrompt.Enabled and KeycardAccess:FindFirstChild(KeycardRank).Value then -- Granted
			ProximityPrompt.Enabled = false
			
			KeycardReader.KeyCardReader.Light.Color = COLOR_SCHEME.Success
			PlaySoundAsync(KeycardGrantedSFX, SoundBlock)
			local Collisions = VehiclePosition.Colision:GetTouchingParts()
			if #Collisions == 0 then
				--Sucess 
				SpawnVehicle(VehicleType, VehiclePosition:GetPrimaryPartCFrame()) -- Awaits a return
				
			else
				NotifyEvent:FireClient(Player, "Interaction Failed", "Please ensure that spawning area is empty")
			end
			wait(3)
			KeycardReader.KeyCardReader.Light.Color = COLOR_SCHEME.Default
			ProximityPrompt.Enabled = true
			Ready = true
		else --Denied
			KeycardReader.KeycardReader.Light.Color = COLOR_SCHEME.Failed
			PlaySoundAsync(KeyCardDeniedSFX, SoundBlock)
			wait(2)
			KeycardReader.KeycardReader.Light.Color = COLOR_SCHEME.Default

			Ready = true
			return 
		end
	end
	ProximityPrompt.Triggered:Connect(Triggered)
end
1 Like

You can use Touched:Connect(function(hit)

Could you elaborate a little?
Should I create a function that interacts with .Touched and .TouchEnded or something else as I am not entirely sure how to implement it in to my code

I think it has something to do with Region3. The thing is that you need to find the area of ​​the Part in which the car is spawned
learn Region3 for a detailed stuff.

I have modified my code a little and tried the solutions suggested above and it didn’t work.

At the end i returned to my previous code and realised that my hitbox part had “CanTouch” disabled.
Well, at least I solved the issue myself and learned Region3 for apparently no reason.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.