Detecting when a player walks into a new room?

A new game called Tweleve Minutes came out and really liked the design of the game and because I’m bored I thought I’d try recreating it currently I’m trying to figure out how I can change my camera when the player enters a new room. I was gonna go with Region3, but I wasn’t exactly sure how to set it up so I just used .Touched the only issue is if you walk to fast the .Touched doesn’t I guess update properly so the camera and stuff doesn’t update.

here’s the code how can I improve it?

local House = workspace.House
local RoomCam = House.RoomCamera
local Debounce = true
local CurrentRoom = "MainRoom"

local CameraPositions = {
	Bedroom = Vector3.new(-26.75, 26.5, -40),
	MainRoom = Vector3.new(6, 26.5, -24),
}

for _,Part in pairs(House:GetDescendants()) do
	if Part:IsA("Part") and string.find(Part.Name,"Region") then
		Part.Touched:Connect(function()
			if Debounce then
				Debounce = false
				if CurrentRoom ~= Part.Parent.Name then
					warn("Entered new room")
					CurrentRoom = Part.Parent.Name
					RoomCam.Position = CameraPositions[Part.Parent.Name] 
				end
				Debounce = true
			end
		end)
	end
end

2 Likes

Use a RenderStepped loop that uses :GetTouchingParts() on the HumanoidRootPart. This way you don’t have to connect a bunch of parts. Also, use a magnitude check to set the camera to the closest region. Lastly, add a debounce of about 0.25 so the camera doesn’t constantly flicker between multiple regions.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local character = player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local rootPart = humanoid.RootPart
rootPart.Touched:Connect(function() end)

local function onCharacterAdded(newCharacter)
	character = newCharacter
	humanoid = character:WaitForChild("Humanoid")
	rootPart = humanoid.RootPart
	rootPart.Touched:Connect(function() end)
	camera.CameraSubject = humanoid
end

local function getClosest(part: BasePart, array): BasePart
	if #array == 0 then return end
	
	local closest: BasePart
	local distance = math.huge
	
	for _, v in next, array do
		local mag = (v.Position - part.Position).Magnitude
		
		if mag < distance then
			closest = v
			distance = mag
		end
	end
	
	return closest
end

local function cameraRegion()
	local regions = {}
	
	for _, v in next, rootPart:GetTouchingParts() do
		if string.find(v.Name, "Region") then
			table.insert(regions, v)
		end
	end
	
	local region = getClosest(rootPart, regions)
	if region then
		camera.CameraType = Enum.CameraType.Scriptable
		camera.CFrame = camera.CFrame:Lerp(region.CameraLook.Value, 0.125)
	else
		camera.CameraType = Enum.CameraType.Custom
	end
end

RunService:BindToRenderStep("Camera Regions", Enum.RenderPriority.Camera.Value - 9, cameraRegion)

player.CharacterAdded:Connect(onCharacterAdded)

Since I had the time and wanted to try it out myself, I made my own script. Gonna test it out.

Update: It works.

CsxhpaNLva

You can use Touched() and TouchEnded() they works pretty well.

Damn thanks a lot! Used what you had sent just slightly rewrote some things and it seems to work well