You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve? Keep it simple and clear!
i want to fix backtracking for room generation - What is the issue? Include screenshots / videos if possible!
backtracking never works and literally nothing ive done helps - What solutions have you tried so far? Did you look for solutions on the Creator Hub?
i mean i have no clue why the issue happens so what am i meant to do
(i have tried random stuff but none of it has helped)
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that itâs easier for people to help you!
local RoomSpawning = {}
local AngleBounds = {
["Min"] = -135,
["Max"] = 135
}
--ok so
--line 92 - 113 is buggy and i cant fix :sob: (lines might have changed)
--i literally have no clue what the issue is
local CurrentTurnAngle = 0
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Modules = ReplicatedStorage.Modules
local Rooms = ReplicatedStorage.Rooms
local RoomModels = ReplicatedStorage.Models.RoomModels
local Tools = Modules.Tools
local TableTools = require(Tools.TableTools)
local SpawnedRooms = workspace.SpawnedRooms
local CurrentlyTriedRooms = {}
local OverlapParameters = OverlapParams.new()
OverlapParameters.FilterType = Enum.RaycastFilterType.Exclude
--Performs a weighted search on a list of rooms to find the next room to spawn
function WeightedSearch(RoomDirectory: Folder?, Bias: {})
Bias = Bias or {}
local ReconstructedTable = {}
local TotalWeight = 0
for _, Room in pairs(RoomDirectory:GetChildren()) do
local BiasTowardsItem = Bias[Room.Name] or 0
local TotalItemWeight = math.max(BiasTowardsItem, Room:GetAttribute("Weight") + BiasTowardsItem)
ReconstructedTable[Room.Name] = TotalItemWeight
TotalWeight += TotalItemWeight
end
local RandomGenerator = Random.new()
local ChosenWeight = RandomGenerator:NextNumber(0, TotalWeight)
local CurrentWeight = 0
local CurrentChoice = RoomDirectory:GetChildren()[1]
for _, PotentialRoomChoice in pairs(RoomDirectory:GetChildren()) do
CurrentWeight += ReconstructedTable[PotentialRoomChoice.Name]
if ChosenWeight <= CurrentWeight then
CurrentChoice = PotentialRoomChoice
break
end
end
return CurrentChoice
end
--Gets all parts which collide with any hitbox of the given room
function GetCollidingParts(PreviousRoom: Model, Room: Model)
local FullCollisionList = {}
local FakeRoom = Room:Clone()
FakeRoom:PivotTo(PreviousRoom.End.CFrame)
OverlapParameters.FilterDescendantsInstances = {PreviousRoom, FakeRoom}
for _, Part in pairs(FakeRoom.Hitboxes:GetChildren()) do
local OverlapList = workspace:GetPartsInPart(Part, OverlapParameters)
FullCollisionList = TableTools.Combine(FullCollisionList, OverlapList)
end
FakeRoom:Destroy()
return FullCollisionList
end
--Chooses a room based off of factors such as collisions and room spawning chance
function ChooseRoom(PreviousRoom: Model, RoomDirectory: Folder?)
local ChosenRoom
if PreviousRoom:GetAttribute("TendencyToStretch") > 1 then
local NewTendencyToStretch = PreviousRoom:GetAttribute("TendencyToStretch") - 1
ChosenRoom = WeightedSearch(RoomDirectory, {[PreviousRoom.Name] = PreviousRoom:GetAttribute("TendencyToStretch")})
if ChosenRoom.Name == PreviousRoom.Name then
ChosenRoom:SetAttribute("TendencyToStretch", NewTendencyToStretch)
end
else
ChosenRoom = WeightedSearch(RoomDirectory)
end
if table.find(CurrentlyTriedRooms, ChosenRoom.Name) then
return ChooseRoom(PreviousRoom, RoomDirectory)
end
table.insert(CurrentlyTriedRooms, ChosenRoom.Name)
if #CurrentlyTriedRooms >= #RoomDirectory:GetChildren() then
CurrentlyTriedRooms = {}
local RoomBeforePrevious = SpawnedRooms:FindFirstChild("Start")
for _, Room in pairs(SpawnedRooms:GetChildren()) do
if Room:GetAttribute("Room") == PreviousRoom:GetAttribute("Room") - 1 then
RoomBeforePrevious = Room
break
end
end
PreviousRoom:Destroy()
return ChooseRoom(RoomBeforePrevious, RoomDirectory)
end
local CollidingParts = GetCollidingParts(PreviousRoom, ChosenRoom)
if #CollidingParts > 0 then
return ChooseRoom(PreviousRoom, RoomDirectory)
end
local StartLookVector = ChosenRoom.Start.CFrame.LookVector
local EndLookVector = ChosenRoom.End.CFrame.LookVector
local RoomAngle = math.atan2(StartLookVector.Z, StartLookVector.X) - math.atan2(EndLookVector.Z, EndLookVector.X)
local RoomAngleInDegrees = math.deg(RoomAngle)
local FutureTurnAngle = CurrentTurnAngle + RoomAngleInDegrees
if FutureTurnAngle > AngleBounds["Max"] or FutureTurnAngle < AngleBounds["Min"] then
return ChooseRoom(PreviousRoom, RoomDirectory)
end
CurrentTurnAngle = FutureTurnAngle
CurrentlyTriedRooms = {}
return ChosenRoom
end
--Adds a random room to the end of the room branch
function RoomSpawning.AppendRoom(PreviousRoom: Model, Type: string)
Type = Type or "Normal"
PreviousRoom = PreviousRoom or SpawnedRooms:FindFirstChild("Start")
local End = PreviousRoom.End
local NextRoom = PreviousRoom:GetAttribute("Room") + 1
local ChosenRoom = ChooseRoom(PreviousRoom, Rooms[Type]):Clone()
ChosenRoom:PivotTo(End.CFrame)
ChosenRoom:SetAttribute("Room", NextRoom)
ChosenRoom.Parent = SpawnedRooms
return ChosenRoom
end
return RoomSpawning