[UNSOLVED] Need help with this generation script

I am trying to make a generation script for the backrooms, but it is acting in VERY odd behaviors. On the X axis, it spawns with an offset, and only spawns going in a positive direction. Here is the code:

local SEGMENTS = {
	s1 = script.Segment1,
	s2 = script.Segment2,
	s3 = script.Segment3,
	s4 = script.Segment4,
	s5 = script.Segment5,
	s6 = script.Segment6,
	s7 = script.Segment7
}


	local visited = {}
	game.Players.PlayerAdded:Connect(function(plr)
		plr.CharacterAdded:Connect(function(char)
			while char.Humanoid.Health > 0 do
				wait()
				for i = -5, 5 do
				for _, v in pairs(SEGMENTS) do
					local segmentToUse = math.random(1, 7)
					local segmentCopy
					if segmentToUse == 1 then
						segmentCopy = SEGMENTS.s1:Clone()
					elseif segmentToUse == 2 then
						segmentCopy = SEGMENTS.s2:Clone()
					elseif segmentToUse == 3 then
						segmentCopy = SEGMENTS.s3:Clone()
					elseif segmentToUse == 4 then
						segmentCopy = SEGMENTS.s4:Clone()
					elseif segmentToUse == 5 then
						segmentCopy = SEGMENTS.s5:Clone()
					elseif segmentToUse == 6 then
						segmentCopy = SEGMENTS.s6:Clone()
					elseif segmentToUse == 7 then
						segmentCopy = SEGMENTS.s7:Clone()
					end
					local zPos = math.floor((char.HumanoidRootPart.Position.Z + i * 30) / 30 + 0.5) * 30 --rounded
					if visited[zPos] == nil then
						visited[zPos] = segmentCopy
						p = visited[zPos]

						p:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0,2,zPos)))

						p.Parent = workspace.Backrooms
					end
					local xPos = math.floor((char.HumanoidRootPart.Position.X + i * 30) / 30 + 0.5) * 30 --rounded
					if visited[xPos] == nil then
						visited[xPos] = segmentCopy
						local q = visited[xPos]
						
						q:SetPrimaryPartCFrame(CFrame.new(Vector3.new(xPos, 2, i)))
						if q.PrimaryPart.Position == p.PrimaryPart.Position then
							q.PrimaryPart.Position.X = q.PrimaryPart.Position.X + 30
						end
						q.Parent = workspace.Backrooms
					end
				end
			end
		end
	end)
end)

I think you meant to use for i = -5, 5, 1 That’s all i can think. Since if your for loop value starts at a negative number and goes to a positive number you need to add the 3rd parameter.

It works fine on the Z axis, it’s just the X axis I’m having problems with, and that isn’t the problem.

Well, I’m not sure if that’s what’s making it weird, But wouldn’t it better if you used Vector3.new() instead of just directly modifying it directly?

q.PrimaryPart.Position = Vector3.new(q.PrimaryPart.Position.X + 30, q.PrimaryPart.Position.Y, q.PrimaryPart.Position.Z)

That didn’t change anything… Unsurprisingly…