I am trying to create a room generation script for my upcoming “Rooms” fangame. However, room generation appears to act odd as the rooms clone onto themselves and overlap with each other.
Here is the Room Generation script as demonstrated in the video (note that this is in ServerScriptService):
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ServerStorage.Events
local Values = ReplicatedStorage.Values
local PreludeRooms = ReplicatedStorage.PreludeRooms:GetChildren()
local RandomGeneration = Random.new()
Events.DoorOpen.Event:Connect(function()
local RoomClone = PreludeRooms[RandomGeneration:NextInteger(1, #PreludeRooms)]:Clone()
RoomClone.Parent = game.Workspace
Values.RoomNumber.Value += 1
Values.CurrentRoom.Value = RoomClone
Values.CurrentRoom.Value:PivotTo(Values.CurrentRoom.Value.PrimaryPart.CFrame.LookVector)
Values.CurrentRoom.Value.PrimaryPart = RoomClone.Entrance
if Values.RoomNumber.Value < 10 then
RoomClone:FindFirstChild("RoomSign").Text.SurfaceGui.RoomNumber.Text = "000"..Values.RoomNumber.Value
elseif Values.RoomNumber.Value < 100 then
RoomClone:FindFirstChild("RoomSign").Text.SurfaceGui.RoomNumber.Text = "00"..Values.RoomNumber.Value
else
RoomClone:FindFirstChild("RoomSign").Text.SurfaceGui.RoomNumber.Text = "0"..Values.RoomNumber.Value
end
end)
Well since the first room worked correctly, I assume it’s just that specific building that has a messed up spawn point (unless the first 2 are always the same)
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ServerStorage.Events
local Values = ReplicatedStorage.Values
local PreludeRooms = ReplicatedStorage.PreludeRooms:GetChildren()
local RandomGeneration = Random.new()
Events.DoorOpen.Event:Connect(function()
local RoomClone = PreludeRooms[RandomGeneration:NextInteger(1, #PreludeRooms)]:Clone()
RoomClone.Parent = game.Workspace
Values.RoomNumber.Value += 1
RoomClone:PivotTo(Values.CurrentRoom.Value.PrimaryPart.CFrame + Values.CurrentRoom.Value.PrimaryPart.CFrame.LookVector)
Values.CurrentRoom.Value = RoomClone
Values.CurrentRoom.Value.PrimaryPart = RoomClone.Entrance
RoomClone:FindFirstChild("RoomSign").Text.SurfaceGui.RoomNumber.Text = ("%04d"):format(Values.RoomNumber.Value)
end)
Edit: For context, you are pivoting after you set the clone, meaning that the thing you are pivoting to is always the same room. What you actually want to do is pivot and then set the clone. Additionally, you want to pivot to the position of the next room plus the look vector so it stacks instead of sending it to the same position.
Your script is slightly starting to work, however, after the 1st generated room (A-0001), it generates a copy but again reversed and mixing in with the lobby
I am starting to notice a gap between the lobby and A-0001 (1st generated room) at the door-frame. I have rotated the “Entrance” part by 180 degrees and had changed the “pivot” line of the code again to your 1st iteration. The rooms are starting to overlay again but with offset.
(The lobby’s Pivot Orientation offset is at 0,180,0)