":PivotTo()" doesn't work as intended, causes overlapping

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)

The Lobby’s PrimaryPart is set to be its Exit. The 1st generated room’s PrimaryPart is set to be its Exit as well.

As shown in the diagram, I have tried to fix this issue by rotating the Exit of the 1st generated room by 180 degrees on the Y-Axis.

2 Likes

Rotate the cframe you pivot to by 180 degrees (CFrame.Angles(0, math.rad(180), 0))

What do you mean, the lobby’s exit, the 1st generated room’s entrance or its exit?

Wherever you use :PivotTo() you give it a cframe, just multiply it by CFrame.Angles(0, math.rad(180), 0)

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)

1 Like

Also, you’re pivoting to the look vector which is probably not intended since it always has a magnitude of 1. It’s not actually a position

also, you could use string.format("%05i", Values.RoomNumber.Value) to pad 0’s to the string

I removed the Magnitude part of the script

Sorry if I’m misunderstanding, but what would magnitude have to do with this?

To be honest, you could easily change the pivot of the buildings and then pivot it directly to the CFrame of the exit.

Try this:

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.


I tried your script, and this happened; the generated room is reversed and mixes in with the lobby

What is meant to happen is that the rooms should spawn in front of each other by their exits

In that case, try flipping it:

RoomClone:PivotTo((Values.CurrentRoom.Value.PrimaryPart.CFrame + Values.CurrentRoom.Value.PrimaryPart.CFrame.LookVector) * CFrame.Angles(0,math.rad(180),0))

PivotTo() ignores things like overlapping and collisions while MoveTo() does not.

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

What I’m thinking is that the first one is the one that is reversed. Try flipping the entrance and change it back to:

RoomClone:PivotTo(Values.CurrentRoom.Value.PrimaryPart.CFrame + Values.CurrentRoom.Value.PrimaryPart.CFrame.LookVector)

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)

Do you mind sending the game file for me to check out?

Nvm, I found the problem. The code wasn’t arranged properly

My new fixed code:

local ServerStorage = game:GetService("ServerStorage")

local Events = ServerStorage.Events
local Values = ServerStorage.Values
local PreludeRooms = ServerStorage.PreludeRooms:GetChildren()

local RandomGeneration = Random.new()

Events.DoorOpen.Event:Connect(function()
	local RoomClone = PreludeRooms[RandomGeneration:NextInteger(1, #PreludeRooms)]:Clone()
	Values.RoomNumber.Value += 1
	RoomClone:PivotTo(Values.CurrentRoom.Value.Exit.CFrame)
	RoomClone.Parent = game.Workspace
	Values.CurrentRoom.Value = RoomClone

	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)

Thank you for your help. Cross-check between my original code and my new fixed code if you want to.

1 Like