Random hallway generation does not spawn correctly

I am creating a script that generates random hallways after the player opens a door. It seems after the first hallway it randomly generates, it spawns far away like its 1 hallway away from the spawn where its suppose to be. I am trying to make the hallways constantly spawn 10 times or as much as I change it to. Here is the video of what happens:


Here is the script:

local RoomsFolder = ReplicatedStorage:WaitForChild("Rooms")
local RemoteEvent = ReplicatedStorage:WaitForChild("Enable")
local TweenService = game:GetService("TweenService")
local Workspace = game:GetService("Workspace")

local generatedRooms = Workspace:FindFirstChild("GeneratedRooms")
if not generatedRooms then
	generatedRooms = Instance.new("Folder")
	generatedRooms.Name = "GeneratedRooms"
	generatedRooms.Parent = Workspace
end

local lastEndCFrame = nil
local roomCount = 0
local maxRooms = 10

local function setupHandleClicks(room)
	local handle = room:FindFirstChild("Handle1", true)
	if not handle then return end

	local clickdetector = handle:FindFirstChildOfClass("ClickDetector")
	if not clickdetector then return end

	clickdetector.MouseClick:Connect(function()
		spawnNextRoom()

		local model = handle:FindFirstAncestorWhichIsA("Model")
		if not model or not model.PrimaryPart then return end
		local hinge = model.PrimaryPart:FindFirstChild("Hinge")
		if not hinge then return end

		local center = hinge.WorldPosition
		local angle = math.rad(80)

		local rootCF = model.PrimaryPart.CFrame
		local rootPos = rootCF.Position
		local offset = rootPos - center
		local rotatedOffset = CFrame.Angles(0, angle, 0):VectorToWorldSpace(offset)
		local newPos = center + rotatedOffset
		local newCFrame = CFrame.new(newPos) * (rootCF - rootPos) * CFrame.Angles(0, angle, 0)

		local tween = TweenService:Create(model.PrimaryPart, TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {CFrame = newCFrame})
		tween:Play()

		local doorSound = model:FindFirstChild("Door", true)
		if doorSound and doorSound:FindFirstChild("Open") then
			doorSound.Open:Play()
		end

		clickdetector.MaxActivationDistance = 0
	end)

	clickdetector.MouseHoverEnter:Connect(function()
		if handle:FindFirstChild("Highlight") then return end
		local highlight = Instance.new("Highlight")
		highlight.Adornee = handle
		highlight.OutlineColor = Color3.fromRGB(255, 255, 255)
		highlight.FillTransparency = 1
		highlight.OutlineTransparency = 1
		highlight.DepthMode = Enum.HighlightDepthMode.Occluded
		highlight.Parent = handle

		local tween = TweenService:Create(highlight, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {OutlineTransparency = 0})
		tween:Play()
	end)

	clickdetector.MouseHoverLeave:Connect(function()
		local highlight = handle:FindFirstChild("Highlight")
		if highlight then
			local tween = TweenService:Create(highlight, TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {OutlineTransparency = 1})
			tween:Play()
			tween.Completed:Wait()
			highlight:Destroy()
		end
	end)
end

function spawnNextRoom()
	if roomCount >= maxRooms then return end
	roomCount += 1

	local room
	if math.random() < 0.5 then
		room = RoomsFolder:FindFirstChild("NormalRoom")
	else
		local filtered = {}
		for _, r in pairs(RoomsFolder:GetChildren()) do
			if r.Name ~= "NormalRoom" then
				table.insert(filtered, r)
			end
		end
		room = #filtered > 0 and filtered[math.random(1, #filtered)] or RoomsFolder:FindFirstChild("NormalRoom")
	end

	if not room then return end

	local newRoom = room:Clone()
	local startPart = newRoom:WaitForChild("Start")
	local endPart = newRoom:WaitForChild("End")
	newRoom.PrimaryPart = startPart

	if lastEndCFrame then
		local relativeCFrame = startPart.CFrame:ToObjectSpace(endPart.CFrame)
		newRoom:SetPrimaryPartCFrame(lastEndCFrame * relativeCFrame)
	else
		newRoom:SetPrimaryPartCFrame(startPart.CFrame)
	end

	newRoom.Parent = generatedRooms
	lastEndCFrame = newRoom:WaitForChild("End").CFrame

	setupHandleClicks(newRoom)
end

RemoteEvent.OnServerEvent:Connect(function()
	spawnNextRoom()
end)
1 Like

Still waiting how to fix this issue

try dividing by 2 in the relativecframe part
i would do smth like this

local endPart = ...
local endCFrame = endPart.Positon
local extendSize = newRoom:Getexendsize() --gets the size
local newCFrame = endCFrame * CFrame.new(0, 0, extendSize.Z/2) --for z axis atleast

(idk about rotation let me think again)

Nope scrap that, instead try a “door” method:
-Have a end and start for each room
-The start should be positioned at the beginning of the room
-The end should be positioned at the end of the room
-when making a new room connect the end of the last room and the start of the last room.
(make sure the start is the primart part and rotated the right way)

1 Like

Thanks it works, ive been trying to fix this issue for quite some time

2 Likes

No problem, glad you got it fixed. Have a nice day on good luck on your game!

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